[LLVMdev] using external functions from llvm

Óscar Fuentes ofv at wanadoo.es
Thu Aug 19 20:29:21 PDT 2010

Alec Benzer <alecbenzer at gmail.com> writes:

>> I'm afraid that you might be utterly confused about how to use LLVM.
> Er, I don't think so? I think I was mostly just confused about interfacing
> with external functions, since whoever wrote that blog post (
> http://www.gearleaf.com/blog/post/44) didn't seem to know what he was
> talking about (or he was doing stuff that once worked but now doesn't? I
> don't know). If you glance at that post I think you'll understand why I was
> doing what I was doing.
> anyway, I think I get it now, what I was trying to do seems to be working
> how I expect it to.

I've just looked at the blog post.

That is over-complicated. There is no need for a wrapping function for
calling external functions. It seems to me that the example on that blog
post is badly broken on several ways.

Instead of sys::DynamicLibrary::AddSymbol use
Module::getOrInsertFunction and then ExecutionEngine::addGlobalMapping
passing to it a pointer to your function. No wrappers needed. Be sure
that the FunctionType matches the signature of your function. Try first
with the simplest case (a function that takes 0 args and returs void)
then with an integer argument, etc. Things become really messy when you
reach C structs (let's forget about C++ classes.)

Finally, follow the standard requirements for setting up the JIT, like
calling InitializaNativeTarget at the beginning of `main'.

My advice is to forget about the example on that blog post and use as an
skeleton some of the examples that comes with LLVM, like HowToUseJIT or

More information about the llvm-dev mailing list