LLVM using external function

I have a function defined in another cpp file that I would like to use in LLVM IR. Could you please tell me how I can use and link them.

I did the following

FunctionType *joinTy = FunctionType::get(voidTy, false);
Function *join = Function::Create(joinTy, Function::ExternalLinkage,"join", &M);
join->setCallingConv(CallingConv::C);

      

And named it as follows:

Function *join = (&M)->getFunction("join");
CallInst * calljoin = CallInst::Create(join,"",branchInst);

      

I have a connect function in external threads.cpp files like

void join() {
        printf("join\n");
        int i;
        for (i = 0; i < NUM_THREADS; i++) {
                if (threads[i]) {
                        pthread_join(threads[i], NULL);
                }   
        }   
}

      

And I have a file .bc

(LLVM IR) which I compile to .s with llc

. I am compiling threads.cpp

up threads.o

with g++ -c threads.cpp

. Now I am trying to link them like

g++ -o exe test.bc threads.o -pthreads

      

I get an error:

undefined link to join

Although I am linking the required file clearly. Any help?

+3


source to share


1 answer


First, g ++ doesn't understand LLVM bitcode (.bc file). Anything that is LLVM IR binary representation, so you cannot link IR to object files.

If you want to link to LLVM, you can use llvm-link. This will require you to also compile your pthreads for LLVM (clang also supports the -pthread option).



This will take you the rest of the way:

LLVM injects pthread functions into IR

+2


source







All Articles