How to specify a custom stdlib directory for LLVM

懵懂的女人 提交于 2021-02-07 14:22:23

问题


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?


回答1:


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.




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/20530105/how-to-specify-a-custom-stdlib-directory-for-llvm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!