Can't link against static library when compili

2019-06-25 09:49发布

问题:

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;
}

回答1:

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


标签: c llvm clang