4

I'm trying to write a simple syntax checker for C code using the frontend available in libclang. Due to deployment concerns, I need to be able to statically link all the libraries in libclang, and not pass around the .so file that has all the libraries.

I'm building clang/llvm from source, and in llvm/Release+Asserts/lib I have a bunch of .a files that I think I should be able to use, but it never seems to work (the linker spews out thousands of errors about missing symbols). However, when I compile it using the libclang.so also present in that directory as follows:

clang main.c -o bin/dlc -I../llvm/tools/clang/include -L../llvm/Release+Asserts/lib/ -lclang

Everything seems to work well.

What is the minimum set of .a files I need to include to make this work? I've tried including absolutely all of the .a files in the build output directory, with them provided to clang/gcc in different orders, without any success. I only need the functions mentioned in libclang's Index.h, but there don't seem to be any resources or documentation on what the various libclang*.a files are for. It would be very helpful to know which files libclang.so pulls in.

kfc9001
  • 41
  • 1
  • 3
  • What if you add `-static` to the linker flags? –  Feb 09 '13 at 08:13
  • I've tried gcc -static -lstatic1 -lstatic2 -Wl,-ldynamic1 -ldyanamic2 as seen in [link](http://stackoverflow.com/questions/4156055/gcc-static-linking-only-some-libraries) to not statically link all of libc (that much I can link dynamically). However, it doesn't seem to help at all, as I still need to know which .a files are necessary. – kfc9001 Feb 09 '13 at 08:15
  • Oh I see. Which symbols does the linker complain about? –  Feb 09 '13 at 08:17
  • It complains about pretty much every symbol imaginable - a lot of C++ methods from what looks like various stages of the compiler. It takes about 30 seconds to print all the errors so I doubt there would be any value if I reproduced it here... – kfc9001 Feb 09 '13 at 08:23

1 Answers1

1

The following is supposed to work, as long the whole project has all static libraries (I counted 116 in my Release/lib directory).

clang main.c -o bin/dlc -I../llvm/tools/clang/include ../llvm/Release/lib/*.a

[edit: clang main.c -o bin/dlc -I../llvm/tools/clang/include ../llvm/Release/lib/libclang.a ../llvm/Release/lib/*.a]

Note that the output binary is not static, so you don't need any -static flag for gcc or ld, if you're using this syntax.

If that doesn't work you might need to list the libraries in order: if some library requires a function available in another library, then it may be necessary to list it first in the command line. See comments about link order at: http://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Link-Options.html#Link-Options

hdante
  • 7,685
  • 3
  • 31
  • 36
  • I tried this out, but I still get errors. I've pasted the [output](https://gist.github.com/anonymous/4950728) from the linker, in case I'm missing something silly. Using either gcc or clang seems to have the same issue. This is on Fedora 17. I'll try guessing at the link order. – kfc9001 Feb 14 '13 at 05:15
  • undefined reference to `std::string::_Rep::_S_empty_rep_storage'. An stdlib function missing. Run clang++ instead of clang. – hdante Feb 24 '13 at 23:07