Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't link against static library when compiling objects from LLVM bitcode.

Tags:

c

llvm

clang

I am developing an LLVM compiler pass. I run a pass in the following way:

  1. Compile to LLVM bitcode

    clang  foo.c -emit-llvm -c -o foo.bc
    
  2. Run foo.bc through opt (The error still occurs without this step)

  3. Compile back to an object file

    clang  -c -o foo.o foo.bc
    
  4. Now foo.o might be part of a static library.

    ar rc libfoo.a foo.o
    
  5. I am unable to link against libfoo.a when all my c files are compiled in this way.

    clang libfoo.a linkme.o -o linkme
    
    linkme.o:linkme.bc:function main: error: undefined reference to 'foo'
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    

Source files:

foo.c:

int foo(int a)
{
    return a;
}

foo.h

int foo(int a);

linkme.c

#include "foo.h"

int main(int argc, char *argv[])
{
   foo(6);

   return 0;
}
like image 707
Eric Hein Avatar asked Jan 30 '13 20:01

Eric Hein


1 Answers

Now I feel silly. It has nothing to do with the .bc files, just the ordering of the arguments.

Works:

clang  linkme.o  libfoo.a -o linkme

Fails:

clang  libfoo.a linkme.o -o linkme
like image 58
Eric Hein Avatar answered Sep 30 '22 04:09

Eric Hein