简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM