<div dir="ltr">Hi,<div>I have a IR file generated by Clang:</div><div><div><br></div><div>; ModuleID = 'test_load_lib.c'</div><div>target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-f80:128:128-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S32"</div>

<div>target triple = "i686-pc-mingw32"</div><div><br></div><div>declare i32 @a_outside_func(i32)</div><div><br></div><div>define i32 @test_func() {</div><div>entry:</div><div>  %call = call i32 @a_outside_func(i32 15)</div>

<div>  ret i32 %call</div><div>}</div></div><div><br></div><div>where a_outside_func is a function defined on my code:</div><div><div><br></div><div>extern "C" int a_outside_func(int a)</div><div>{</div><div>  return a + 50;</div>

<div>}</div></div><div><br></div><div>and following code is to get MCJIT work:</div><div><br></div><div>#define prt(x) if(x) { cout << x << endl; }<br></div><div><div>LLVMInitializeAllTargets();</div><div>  LLVMInitializeAllTargetMCs();</div>

<div>  LLVMInitializeAllTargetInfos();</div><div>  LLVMInitializeAllAsmParsers();</div><div>  LLVMInitializeAllAsmPrinters();</div><div>  LLVMInitializeAllDisassemblers(); // just initialize them all....</div><div>  prt(a_outside_func(50));               // afraid of linker optmize it out</div>

<div><br></div><div>  char *err = 0;</div><div>  LLVMMemoryBufferRef ll_f = 0;</div><div>  LLVMModuleRef m = 0;</div><div>  LLVMCreateMemoryBufferWithContentsOfFile("test_load_lib.ll",&ll_f,&err);   //read .ll</div>

<div>  prt(err);</div><div>  LLVMParseIRInContext(LLVMGetGlobalContext(),ll_f,&m,&err); // ll_f doesnt need freeing</div><div>  prt(err);</div><div>  LLVMDumpModule(m);</div><div><br></div><div>  LLVMLinkInMCJIT();</div>

<div>  LLVMExecutionEngineRef ee = 0;</div><div>  LLVMCreateMCJITCompilerForModule(&ee,m,0,0,&err);</div><div>  prt(err);</div><div>  using tf_t = int ();</div><div>  tf_t *f = (tf_t*)LLVMGetPointerToGlobal(ee,LLVMGetNamedFunction(m,"test_func"));</div>

</div><div><br></div><div>At first i got "LLVM ERROR: Incompatible object format! "</div><div>But by reading some articles I append LLVMSetTarget(m,"i686-pc-mingw32"); and it's fixed.</div><div>Then I got "LLVM ERROR: Program used external function 'a_outside_func' which could not be resolved!".<br>

</div><div>I assumed my program didn't export the symbol properly.Thus,I use nm to check my program's export and I found "a_outside_func" was renamed to "_a_outside_func"</div><div>.</div><div>

For it I rename the function name in the IR to "_a_outside_func".But it seemed to make no difference: "LLVM ERROR: Program used external function '_a_outside_func' which could not be resolved!".</div>

<div>.............</div><div>After a day's wasting I unconsciously packed my external function "a_outside_func" into a shared object,called llvm::sys::DynamicLibrary::LoadLibraryPermanently("ext_func.so"); at the beginning of the program and named the function "_a_outside_func" in the IR.</div>

<div>OMG IT WORKS THEN?!</div><div>So I'd like to ask why llvm cant resolve my external function from the program itself(in fact i dont get such problems on linux)?</div></div>