0

I have a library foo in foo.c:

int foo() { return 0; }

I want to compile to a static objectfoo.o. When I do it directly like the following, this works.

clang -c foo.c -o foo.o

However, I want to go via the llvm byte code:

clang -emit-llvm -c foo.c  # Compile to LLVM byte code
clang foo.bc -o foo.o      # Compile LLVM byte code to native

The last command fails with following error message:

/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
clang-3.9: error: linker command failed with exit code 1 (use -v to see invocation)

I know that there is no main, which is expected since I compile a library. How to compile that library from LLVM byte code?

Razer
  • 7,843
  • 16
  • 55
  • 103
  • I think you’re adding the `-c` to the wrong line in the LLVM case. What happens if you run `clang foo.bc -c -o foo.o`? Not an answer because I’m not at all sure I’m right; it’s just a guess. – Daniel H Feb 07 '17 at 15:43
  • This might help: http://stackoverflow.com/questions/21907504/how-to-compile-shared-lib-with-clang-on-osx – deLta Feb 07 '17 at 16:54

1 Answers1

0

As Daniel H mentions in comment,

the compilation commands should be:

clang -emit-llvm -c -o foo.bc foo.c
clang -c foo.bc -o foo.o

then it is okay to make a library with foo.o, I have tested it on clang9.

Hope this helps.

CK98
  • 46
  • 4