2

I'm building a project using cmake which uses Google's dense hash maps. I need to check that the corresponding header file is present. I therefore added

set (CMAKE_CXX_STANDARD 11)
INCLUDE (CheckIncludeFileCXX)
CHECK_INCLUDE_FILE_CXX("sparsehash/dense_hash_map" HAVE_SPARSEHASH)

to my CMakeLists.txt. However, it fails to find the header when I compile on some old compiler. The problem is that the header must be compiled in C++11 and these compiler are not C++11 by default:

/usr/bin/clang++      -o CMakeFiles/cmTC_4186d.dir/CheckIncludeFile.cxx.o -c /home/florent/src/IVMPG/HPCombi/build/CMakeFiles/CMakeTmp/CheckIncludeFile.cxx
In file included from /home/florent/src/IVMPG/HPCombi/build/CMakeFiles/CMakeTmp/CheckIncludeFile.cxx:1:
In file included from /usr/local/include/sparsehash/dense_hash_map:102:
In file included from /usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../../include/c++/7/type_traits:35:

/usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../../include/c++/7/bits/c++0x_warning.h:32:2: error: This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support \
^

As a consequence CMake think the file is not present.

So the question is: How do I ensure that I'm using c++11 (or even c++14) when I'm testing some include or symbols ?

1 Answer 1

3

Macro CHECK_INCLUDE_FILE_CXX internally uses try_compile, which preserves compiler flags in CMAKE_CXX_COMPILE_FLAGS variable, but, until CMake 3.8, it didn't preserve language standards (like CMAKE_CXX_STANDARD variable).

So, if you have CMake version 3.7 or lower, the only way to ensure checks to use c++11, is to add standard-related option to CMAKE_REQUIRED_FLAGS variable before call to CHECK_INCLUDE_FILE_CXX:

set(CMAKE_REQUIRED_FLAGS "-std=c++11")
...
CHECK_INCLUDE_FILE_CXX("sparsehash/dense_hash_map" HAVE_SPARSEHASH)

Yes, cross-platform aspects of CMAKE_CXX_STANDARD are broken in that case.

If you have CMake version 3.8 or higher, default behavior of CMake is still to ignore language standards. This is done for compatibility with older versions. For preserving language standards, set policy CMP0067 to NEW:

cmake_policy(SET CMP0067 NEW)
...
CHECK_INCLUDE_FILE_CXX("sparsehash/dense_hash_map" HAVE_SPARSEHASH)
Sign up to request clarification or add additional context in comments.

1 Comment

I got stuck for pretty long on this one on check_cxx_source_compiles - it turns out that you have to specify the policy before you include(CheckCXXSourceCompiles). Changing the policy after has no effect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.