How to specify a custom stdlib directory for LLVM

7.8k views Asked by At

I have LLVM 3.3 with Clang, and

$ /tmp/clang/bin/clang -print-search-dirs
programs: =/tmp/clang/bin:/usr/lib/gcc/i486-linux-gnu/4.4/../../../../i486-linux-gnu/bin
libraries: =/tmp/clang/bin/../lib/clang/3.3:/usr/lib/gcc/i486-linux-gnu/4.4:/usr/lib/gcc/i486-linux-gnu/4.4/../../../../lib32:/usr/lib/../lib32:/usr/lib/i486-linux-gnu/../../lib32:/usr/lib/gcc/i486-linux-gnu/4.4/../../..:/lib:/usr/lib

How can I instruct Clang to usage an stdlib (e.g. libgcc) directory other than /usr/lib/gcc/i486-linux-gnu/4.4? I'd like to make it use /tmp/mygccstd instead.

It's also looking in /usr/lib and /lib. How do I disable that?

3

There are 3 answers

4
pts On BEST ANSWER

A combination of -B and --sysroot did the trick for the libraries. A combination of -nostdinc, -isystem and -cxx-isystem did the trick for the includes. Not all these flags are displayed by clang --help, some of them I learned from man gcc, some other reading the Clang source code, and some other online.

-gcc-toolchain also made a difference, but it was using weird rules to find the libraries, disallowing symlinks in the pathname components etc., so I ended up using the other flags above instead.

0
puio On

This is documented in the libcxx docs:

clang++ -std=c++17 -stdlib=libc++ -nostdinc++ \
          -I<libcxx-install-prefix>/include/c++/v1 \
          -L<libcxx-install-prefix>/lib \
          -Wl,-rpath,<libcxx-install-prefix>/lib \
          test.cpp

<libcxx-install-prefix> will be the <location of clang binary>/../../

This stops the compiler from using system dylib.

1
TheBat On

On my system I have 3 compilers installed. gcc-7.3.0, gcc-7.2.0, and clang-6.0

gcc-7.3.0 is installed to the system path and is the system default compiler.

gcc-7.2.0 is installed to /usr/local and is a build requirement for a specific tool.

clang-6.0 is installed to /usr/local and is used for it's stricter warnings/errors.

My boost libraries are compiled with gcc-7.2.0 and I wished to use clang to compile my specific tool. By default, with -stdlib=libstdc++ clang would find gcc-7.3.0 and boost would fail to link.

To get around this I used the following compile flags:

-stdlib=libstdc++ # Tell clang to parse the headers as libstdc++ not libc++
-cxx-isystem /usr/local/include/c++/7.2.0/ # includes for libstdc++
-cxx-isystem /usr/local/include/c++/7.2.0/x86_64-pc-linux-gnu/ # includes for libstdc

And the following linker flags:

-L/usr/local/lib64/ # static libstdc++
-L/usr/local/lib/gcc/x86_64-pc-linux-gnu/7.2.0/ #static libgcc

You can fill in your own linker paths with the directories that hold libstdc++.a and libgcc.a and these will depend on where your compiler is installed to.