我正在使用 cmake 构建一个项目,该项目使用 Google 的密集 HashMap 。我需要检查相应的头文件是否存在。因此我添加了
set (CMAKE_CXX_STANDARD 11)
INCLUDE (CheckIncludeFileCXX)
CHECK_INCLUDE_FILE_CXX("sparsehash/dense_hash_map" HAVE_SPARSEHASH)
到我的CMakeLists.txt
。但是编译时找不到头文件
在一些旧的编译器上。问题是头文件必须编译在
C++11 和这些编译器默认不是 C++11:
/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 \
^
因此,CMake 认为该文件不存在。
所以问题是:如何确保我使用的是 c++11
(甚至是 c++14
)
当我测试一些包含或符号时?
最佳答案
宏 CHECK_INCLUDE_FILE_CXX
内部使用 try_compile ,它将编译器标志保留在 CMAKE_CXX_COMPILE_FLAGS 变量中,但是,直到 CMake 3.8,它 didn't preserve 语言标准 (例如CMAKE_CXX_STANDARD 变量)。
因此,如果您的 CMake 版本为 3.7 或更低版本,确保检查使用 c++11 的唯一方法是在调用 之前向 CMAKE_REQUIRED_FLAGS 变量添加标准相关选项CHECK_INCLUDE_FILE_CXX
:
set(CMAKE_REQUIRED_FLAGS "-std=c++11")
...
CHECK_INCLUDE_FILE_CXX("sparsehash/dense_hash_map" HAVE_SPARSEHASH)
是的,在这种情况下,CMAKE_CXX_STANDARD 的跨平台方面就被破坏了。
如果您有 CMake 3.8 或更高版本,CMake 的默认行为仍然是忽略语言标准。这样做是为了与旧版本兼容。为了保留语言标准,请将策略 CMP0067 设置为NEW:
cmake_policy(SET CMP0067 NEW)
...
CHECK_INCLUDE_FILE_CXX("sparsehash/dense_hash_map" HAVE_SPARSEHASH)
关于cmake 在检查头文件时使用 corrext c++ 标准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47213356/