gcccmakedevtoolset

CMake uses gcc from devtoolset but links against wrong libgcc and libstd++


I have a project with CMake, devtoolset-6 and standart gcc 4.8.2 installed. CMake correctly finds correct gcc:

-- The C compiler identification is GNU 6.2.1
-- The CXX compiler identification is GNU 6.2.1
-- Check for working C compiler: /opt/rh/devtoolset-6/root/usr/bin/cc
-- Check for working C compiler: /opt/rh/devtoolset-6/root/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /opt/rh/devtoolset-6/root/usr/bin/c++
-- Check for working CXX compiler: /opt/rh/devtoolset-6/root/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done

PATH and LD_LIBRARY_PATH are set

echo $PATH
/opt/rh/devtoolset-6/root/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
echo $LD_LIBRARY_PATH
/opt/rh/devtoolset-6/root/usr/lib64:/opt/rh/devtoolset-6/root/usr/lib

The project builds correctly but lings against wrong libgcc and libstd++ from /usr/lib64

  libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007f60b47bf000)
  libgcc_s.so.1 => /usr/lib64/libgcc_s.so.1 (0x00007f60b45a8000)

What is wrong?


Solution

  • This is not a bug, but the feature! :) RH DevToolset is not just an alternative compiler built on the side. It's libstdc++ is an LD script. The new C++11 (and above) symbols in your binary would be resolved via static library (built specially and shipped w/ DTS). However, old symbols (existed in the OS's shipped libstdc++) going to be resolved dynamically. That is why ldd shows you libstdc++ from the /usr/lib64! Also, it has Old C++ ABI by default... to make your modern C++ code compatible w/ old libstdc++ DSO.

    This feature allows you to use modern C++ standards, and produce a binary which can be executed in the original OS (CentOS/RHEL) w/o any additional "redistributable" shared libraries.

    JFYI, if you use strip, make sure you use it from DTS and not the "native" one (shipped in binutils from the OS)! Otherwise, really bad things can happen.