[LLVMdev] LLVM-C: Calling functions contained in other libraries

F van der Meeren llvm-dev at code2develop.com
Thu Aug 12 10:43:23 PDT 2010


I have a question concerning llvm-c.
I have set up a function that needs to invoke an external method, in a other library. It has the following signature: void* NSFullUserName(void);
The void* can be replaced with a i8*, that far I was able to get, but when my call is invoked, the engine gives me the following message:

LLVM ERROR: Tried to execute an unknown external function: i8* ()* NSFullUserName

I have dumped the module (without the clutter):

declare i8* @NSFullUserName()

define i8* @MyFunction() {
  %myCall = call i8* @NSFullUserName()            ; <i8*> [#uses=1]
  ret i8* %myCall

Where am I going wrong here?

To add some code to my previous question:

LLVMTypeRef i8Ptr(void) {
    return LLVMPointerType(LLVMInt8Type(), 0);

LLVMValueRef d(LLVMModuleRef module) {
    LLVMValueRef result;
    LLVMBasicBlockRef block;
    LLVMBuilderRef builder = LLVMCreateBuilder();
    LLVMValueRef fullUsername = LLVMAddFunction(module, "NSFullUserName", LLVMFunctionType(LLVMPointerType(LLVMInt8Type(), 0), NULL, 0, 0));
    LLVMSetLinkage(fullUsername, LLVMExternalLinkage);
    result = LLVMAddFunction(module, "MyFunction", LLVMFunctionType(i8Ptr(), NULL, 0, 0));
    block = LLVMAppendBasicBlock(result, "entrypoint");
    LLVMPositionBuilderAtEnd(builder, block);
    LLVMValueRef returnVal = LLVMBuildCall(builder, fullUsername, NULL, 0, "myCall");
    LLVMBuildRet(builder, returnVal);
    return result;

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    char * error = NULL;
    LLVMExecutionEngineRef engine;
    LLVMModuleRef module = LLVMModuleCreateWithName("MyModule");
    LLVMValueRef toCall = d(module);
    LLVMCreateInterpreterForModule(&engine, module, &error);
    LLVMGenericValueRef result = LLVMRunFunction(engine, toCall, 0, NULL);
    [pool drain];
    return 0;

Thank you,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100812/388c33da/attachment.html>

More information about the llvm-dev mailing list