Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting a specific libstdc++ version with clang

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.

like image 699
Andy Thomas Avatar asked Nov 08 '16 11:11

Andy Thomas


People also ask

Does Clang use Libstdc ++?

Clang supports use of either LLVM's libc++ or GCC's libstdc++ implementation of the C++ standard library.

Which is faster GCC or Clang?

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.

Are GCC and Clang interchangeable?

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").

How to check if Clang/libstdc++ C++ support?

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.

How to link boost with libstdc++?

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).

Is Libstdc++ known to all compilers?

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.

Why doesn't Clang use its own standard library?

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.


1 Answers

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 ...
like image 136
fwyzard Avatar answered Oct 15 '22 01:10

fwyzard