Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a custom stdlib directory for LLVM

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?

like image 837
pts Avatar asked Oct 20 '22 19:10

pts


2 Answers

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.

like image 57
TheBat Avatar answered Oct 24 '22 01:10

TheBat


This is documented in the libcxx docs:

  • https://libcxx.llvm.org/docs/UsingLibcxx.html
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.

  • How to specify custom libc++
  • When running clang built from source, how to specify location of libc++, or, someone explain to me what -stdlib=libc++ does
like image 37
puio Avatar answered Oct 23 '22 23:10

puio