coder hacker
coder hacker

Reputation: 4868

LLVM using external function

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

I have done the following

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

And called it as follows:

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

I have the join function in external file threads.cpp 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 .bc (LLVM IR) file I compile to .s using llc . I compile threads.cpp to threads.o using g++ -c threads.cpp. Now I am trying to link them as

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

I am getting error:

undefined reference to join

Even though I am clearly linking the required file. Any help?

Upvotes: 4

Views: 5357

Answers (1)

ScottyP
ScottyP

Reputation: 154

First off, g++ doesn't understand LLVM's bitcode (the .bc file). All that is is an binary representation of the LLVM IR so you can't link IR with object files.

If you want to do linking with LLVM you can use llvm-link. That will require that you have also compiled your pthreads to LLVM (clang supports the -pthread option as well).

This should take you the rest of the way:

LLVM insert pthread function calls into IR

Upvotes: 3

Related Questions