title: Linking LLVM JIT code to external C++ functions
body: <p>I'm writing a LLVM scripting engine that JIT compiles scripting code in a custom language. My problem is that I'm unable to call external functions (even the C99 erf() function is failing).</p>
  
  <p>For example if I extern "C" the erf function,</p>
  
  <pre><code>extern "C" double erft(double x){
  return erf(x);
  }
  </code></pre>
  
  <p>and create a function with external linkage</p>
  
  <pre><code>std::vector&lt;const Type*&gt; Double1(1,Type::getDoubleTy(getGlobalContext()));
  FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),Double1,false);
  Function *erft = Function::Create(FT,Function::ExternalLinkage,"erft",TheModule);
  </code></pre>
  
  <p>get the following error message when running my script with erft(0.0) : </p>
  
  <blockquote>
    <p>LLVM ERROR: Program used external function 'erft' which could not be resolved!</p>
  </blockquote>
  
  <p>Doing the mapping manually, </p>
  
  <pre><code>void ExecutionEngine::addGlobalMapping( const GlobalValue *  erfF, void *  erft); 
  </code></pre>
  
  <p>will get me the following error: </p>
  
  <blockquote>
    <p>declaration of `void llvm::ExecutionEngine::addGlobalMapping(const llvm::GlobalValue*, void*)' outside of class is not definition</p>
  </blockquote>
  
  <p>Obviously I'm doing something very wrong. Any help would be much appreciated</p>