I would like to ask you on the proper way to call/bind own c++ function into LLVM.
I have coded simple function:
void writeSomething() {
    std::cout << "Awesome" << std::endl;
} 
In LLVM I am trying to register the function. I have created the external linkage to it.
      // Void type
llvm::FunctionType* fccType =
        llvm::FunctionType::get(
            llvm::Type::getVoidTy(getGlobalContext()), false
        );
// External - c++
Function *fcc = (Function*) module->getOrInsertFunction("writeSomething",
        fccType
        );
// Call
std::vector<Value*> emptyArgs;
CallInst::Create(fcc, makeArrayRef(emptyArgs));
LLVM Output for just calling this function is ( // comments are mine input how do I understand the output )
// External linkage
declare void @writeSomething()
define internal void @main() {
entry:
  // Call my function
  call void @writeSomething()
  ret void
}
The program ends with message: LLVM ERROR: Program used external function 'writeSomething' which could not be resolved!
Due to C++ name mangling, the name of that function is actually something like _Z14writeSomethingv - C++ supports overloading by encoding type information in the function name.
You can disable this by declaring the function as extern "C" void writeSomething() { ... }, or figure out what it should be called under your compiler's name mangling scheme and use that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With