Including correct versions of STL headers through CMake Toolchains

I am trying to set up a CI pipeline to use with C++. Docker containers I have do not support C++17 so I wanted to download a release from LLVM page and set up the thing properly. I then extract this under /opt/clang7. I also use a CMake toolchain file so that it will pick up includes and libs etc from this clang distribution. Here is my toolchain file clang7-ci.cmake:

SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)

SET(CMAKE_C_COMPILER   /opt/clang7/bin/clang)
SET(CMAKE_CXX_COMPILER /opt/clang7/bin/clang++)

SET(CMAKE_FIND_ROOT_PATH  /opt/clang7)

SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

I call the CMake configuration as cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/clang7-ci.cmake .. and it seems to be picking up correct clang and clang++ from the toolchain. As I can see them in configuration output of CMake as:

-- The C compiler identification is Clang 7.0.1

-- The CXX compiler identification is Clang 7.0.1

-- Check for working C compiler: /opt/clang7/bin/clang

-- Check for working C compiler: /opt/clang7/bin/clang -- works

So, I assume it is using toolchain directory correctly, otherwise how would it find my set C++ compiler as clang++. Still, it is giving me the below error when I try to build with cmake --build .:

In file included from /builds/meguli/chops/tests/answer_life/answer_life.cpp:2: /builds/meguli/chops/tests/answer_life/../catch.hpp:644:10: error: no template named 'is_same_v' in namespace 'std'; did you mean 'is_same'?

           (!std::is_same_v<T, Rest> && ...) && is_unique<Rest...>
              ~~~~~^

/usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/type_traits:1517:12: note: 'is_same' declared here struct is_same ^

As you can see, it is looking at host directory at /usr/lib/.../inclue/6.3.0 for include files. This is not what I wanted, I want to build for C++17 and I want it to include headers from clang's distribution. I set SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) so that it would search the toolchain for include directories but it is not working. How can I make it search only under /opt/clang7?

728x90

1 Answers Including correct versions of STL headers through CMake Toolchains

Instead of mutating CMAKE_CXX_FLAGS manually (you shouldn't!) Try setting the C++ standard used by cmake:

set(CMAKE_CXX_STANDARD 17)

Put this before defining targets.

Then, clang will by default use libstdc++ by default. To make every C++17 features available, simply upgrade GCC.

3 weeks ago