How to specify a custom stdlib directory for LLVM
Asked Answered
D

3

7

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?

Destroy answered 11/12, 2013 at 21:17 Comment(1)
I could find find the -gcc-toolchain flag, which changes programs: and parts of libraries:.Destroy
D
-1

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.

Destroy answered 16/12, 2013 at 20:46 Comment(4)
This 'answer' does not really constitute a solution to the original question, as all it really says is "I used some flags". A sample working solution is, I think, the minimum for an accepted response.Graphophone
I agree with you that this answer doesn't contain a sample working solution. The merit of this answer is that it reveals which the useful flags are (among 100s), and it's also a testimony that it's possible to solve it with these flags. I strongly disagree with your opinion that this answer really says "I used some flags". This answer is actually helpful and useful, while "I used some flags" doesn't help at all. I disagree with you that a sample working solution should be the minimum for an accepted answer.Destroy
@pts, my clang is unable to recognize some header files, specifically <bits.stdc++.h>. But the program compiles fine with g++-5 (custom gcc) on my Mac. So exactly, what command do I have to give to make it look for the header files in the path g++-5 is searching? I have the output of g++-5 -print-search-dirsOrotund
@manish_s: I'd try -I and/or -isystem with the directory containing bits.stdc++.h. Please note that this question is not about adding paths, but it's about hiding default (system) paths, which is very different from your question. You may want to ask your question separately on StackOverflow.Destroy
W
3

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.

Wildeyed answered 26/3, 2019 at 15:43 Comment(1)
This is rare advice, and did the trick for me. Thanks. Trivially, I wonder why you choose -cxx-isystem rather than -isystem? With the issue I was looking at, either way solved it. I also now found discussion on -stdlibc++-isystem at reviews.llvm.org/D64089, but using that gave me a fatal error through not finding bits/c++config.h.Hawkbill
S
2

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.

Salmonberry answered 25/11, 2020 at 11:52 Comment(0)
D
-1

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.

Destroy answered 16/12, 2013 at 20:46 Comment(4)
This 'answer' does not really constitute a solution to the original question, as all it really says is "I used some flags". A sample working solution is, I think, the minimum for an accepted response.Graphophone
I agree with you that this answer doesn't contain a sample working solution. The merit of this answer is that it reveals which the useful flags are (among 100s), and it's also a testimony that it's possible to solve it with these flags. I strongly disagree with your opinion that this answer really says "I used some flags". This answer is actually helpful and useful, while "I used some flags" doesn't help at all. I disagree with you that a sample working solution should be the minimum for an accepted answer.Destroy
@pts, my clang is unable to recognize some header files, specifically <bits.stdc++.h>. But the program compiles fine with g++-5 (custom gcc) on my Mac. So exactly, what command do I have to give to make it look for the header files in the path g++-5 is searching? I have the output of g++-5 -print-search-dirsOrotund
@manish_s: I'd try -I and/or -isystem with the directory containing bits.stdc++.h. Please note that this question is not about adding paths, but it's about hiding default (system) paths, which is very different from your question. You may want to ask your question separately on StackOverflow.Destroy

© 2022 - 2024 — McMap. All rights reserved.