Selecting a specific libstdc++ version with clang
Asked Answered
M

1

12

I use clang++ for compiling C++ code. I link against the gcc standard C++ library, libstdc++. However, I have several different installation of libstdc++ on my Ubuntu machine. When I run clang++, it uses the 4.8 installation of libstdc++:

andy@andy:~$ clang++-3.5 -v
Ubuntu clang version 3.5.0-4ubuntu2~trusty2 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
Target: i386-pc-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/4.6
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/4.6.4
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/4.8
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/4.8.4
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/4.9
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/4.9.3
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.6
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.6.4
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.8.4
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.9.3
Selected GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/4.8
Candidate multilib: .;@m32
Selected multilib: .;@m32

How can I tell clang++ to use a different version of the libstdc++ library and headers? specifically, I want to use the 4.6 version.

Mealy answered 8/11, 2016 at 11:43 Comment(4)
Have you tried to use an old version of clang?Joe
@NarekAtayan In a similar system where gcc 4.8 is not installed, the same version of clang (specifically 3.4) uses gcc 4.6. So it is possible without using a different version of clang.Mealy
Hmm, could be impossibleAromatic
Have you tried using --gcc-toolchain=/path/to/gcc/prefix command line option? As mentioned here, it should make Clang use specific version of GCC toolchain, including the libstdc++ along with clang++: "Clang will look within its own prefix for libstdc++ and use it if found. You can also add an explicit prefix for Clang to look in for a GCC toolchain with the --gcc-toolchain=/opt/my/gcc/prefix flag, passing it to both compile and link commands".Enjoin
X
11

As you have seen from the output of clang++ -v, it will default to the latest installation of gcc it finds in a given "prefix" (i.e. installation directory).

The problem with the way multiple versions of gcc are installed e.g. on Ubuntu is that they all use the same prefix, /usr.

So, the workaround I am using is to create a bunch of fake installations, each under its own prefix: /usr/local/gcc/5.5.0, /usr/local/gcc/6.4.0, /usr/local/gcc/7.3.0, etc.:

VERSION=6.4.0
sudo mkdir -p /usr/local/gcc/$VERSION/include/c++
sudo ln -s /usr/include/c++/$VERSION /usr/local/gcc/$VERSION/include/c++/$VERSION
sudo mkdir -p /usr/local/gcc/$VERSION/lib/gcc/x86_64-unknown-linux-gnu
sudo ln -s /usr/lib/gcc/x86_64-linux-gnu/$VERSION /usr/local/gcc/$VERSION/lib/gcc/x86_64-unknown-linux-gnu/$VERSION

Now I can instruct clang++ to use a specific version of gcc's libraries with the --gcc-toolchain option:

clang++ --gcc-toolchain=/usr/local/gcc/6.4.0 ...
Xenophobe answered 21/6, 2018 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.