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.
Clang supports use of either LLVM's libc++ or GCC's libstdc++ implementation of the C++ standard library.
Clang is much faster and uses far less memory than GCC. Clang aims to provide extremely clear and concise diagnostics (error and warning messages), and includes support for expressive diagnostics. GCC's warnings are sometimes acceptable, but are often confusing and it does not support expressive diagnostics.
TL;DR: Clang is highly compatible to GCC - just give it a go. In most cases, Clang could be used as a GCC drop in replacement ( clang and clang++ are "GCC compatible drivers").
That being said, there are various ways to check Clang/libstdc++ C++ support: Clang has the __has_feature macro (and friends) that can be used to detect language features and language extenstions. Libstdc++ has its own version macros, see the documentation. You'll need to include a libstdc++ header to get these defined though.
The key idea is to have libstdc++ as the default linked in stdlib, building as usual. Linking Boost against libc++ then requires versioning this specific build with --layout=versioned. This will add suffices -clangN-x64 to your Boost library files ( N = clang version).
It is known to majority of compilers. You should also use C_INCLUDE_PATH and/or CPLUS_INCLUDE_PATH. These two a more gcc specific (other compilers prefer INCLUDE without language separation). You can also ignore the environment variables completely and specify the correct libstdc++ directly in the command line.
I find this problematic because Clang by default does not use its own Standard Library implementation: it uses libstdc++. While Clang has predefined preprocessor macros __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__, they are hardcoded to values 4, 2, 1 respectively, and they tell me little about the available libstdc++ features.
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With