cmake:为啥 CMAKE_CXX_STANDARD 似乎不适用于 check_cxx_source_compiles

Posted

技术标签:

【中文标题】cmake:为啥 CMAKE_CXX_STANDARD 似乎不适用于 check_cxx_source_compiles【英文标题】:cmake: why doesn't CMAKE_CXX_STANDARD seem to work with check_cxx_source_compilescmake:为什么 CMAKE_CXX_STANDARD 似乎不适用于 check_cxx_source_compiles 【发布时间】:2016-11-29 23:34:44 【问题描述】:

这是一个 MCVE:

cmake_minimum_required(VERSION 3.1)
Project(Test)

include(CheckCXXSourceCompiles)

set (CMAKE_CXX_FLAGS "-std=c++11 $CMAKE_CXX_FLAGS")
#set (CMAKE_CXX_STANDARD_REQUIRED TRUE)
#set (CMAKE_CXX_STANDARD 11)
#set (CMAKE_CXX_EXTENSIONS FALSE)

check_cxx_source_compiles("
#include <atomic>

int main() 
  std::atomic<int> u5;
  return u;
" HAVE_STDLIB_ATOMIC)

if (NOT HAVE_STDLIB_ATOMIC)
  message(FATAL_ERROR "Did not find std::atomic support!")
endif()

当我使用CMAKE_CXX_FLAGS 版本时,它工作正常,但是当我使用我们现在应该使用的新CMAKE_CXX_STANDARD 标志时,它不起作用,我收到以下构建错误:

$ cmake ..
-- The C compiler identification is GNU 5.4.1
-- The CXX compiler identification is GNU 5.4.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /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: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Performing Test HAVE_STDLIB_ATOMIC
-- Performing Test HAVE_STDLIB_ATOMIC - Failed
CMake Error at CMakeLists.txt:20 (message):
  Did not find std::atomic support!


-- Configuring incomplete, errors occurred!
See also "/home/chris/cmake_test/build/CMakeFiles/CMakeOutput.log".
See also "/home/chris/cmake_test/build/CMakeFiles/CMakeError.log".

错误日志表明它没有使用-std=c++11 标志:

$ cat /home/chris/cmake_test/build/CMakeFiles/CMakeError.log 
Performing C++ SOURCE FILE Test HAVE_STDLIB_ATOMIC failed with the following output:
Change Dir: /home/chris/cmake_test/build/CMakeFiles/CMakeTmp

Run Build Command:"/usr/bin/make" "cmTC_42a05/fast"
/usr/bin/make -f CMakeFiles/cmTC_42a05.dir/build.make CMakeFiles/cmTC_42a05.dir/build
make[1]: Entering directory '/home/chris/cmake_test/build/CMakeFiles/CMakeTmp'
Building CXX object CMakeFiles/cmTC_42a05.dir/src.cxx.o
/usr/bin/c++     -DHAVE_STDLIB_ATOMIC   -o CMakeFiles/cmTC_42a05.dir/src.cxx.o -c /home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx
In file included from /usr/include/c++/5/atomic:38:0,
                 from /home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx:2:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #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 \
  ^
/home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx: In function ‘int main()’:
/home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx:5:3: error: ‘atomic’ is not a member of ‘std’
   std::atomic<int> u5;
   ^
/home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx:5:15: error: expected primary-expression before ‘int’
   std::atomic<int> u5;
               ^
/home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx:6:10: error: ‘u’ was not declared in this scope
   return u;
          ^
CMakeFiles/cmTC_42a05.dir/build.make:65: recipe for target 'CMakeFiles/cmTC_42a05.dir/src.cxx.o' failed
make[1]: *** [CMakeFiles/cmTC_42a05.dir/src.cxx.o] Error 1
make[1]: Leaving directory '/home/chris/cmake_test/build/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_42a05/fast' failed
make: *** [cmTC_42a05/fast] Error 2

Source file was:

#include <atomic>

int main() 
  std::atomic<int> u5;
  return u;

我的cmake 版本是:

$ cmake --version
cmake version 3.5.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

我不明白为什么cmake 在这里不使用std=c++11 标志,根据文档,CMAKE_CXX_STANDARD 应该可以工作since version 3.1。

有人知道这里有什么问题吗?

最合适的解决方法是什么?我应该对测试使用CMAKE_CXX_FLAGS 调整,对目标使用CMAKE_CXX_STANDARD 吗?在我看来,测试和目标应该使用完全相同的配置,否则测试的意义何在:/

【问题讨论】:

注意:我发现这个问题目前在他们的问题跟踪器上被跟踪,但如果你能说明一个好的修复或解决方法,我会接受答案gitlab.kitware.com/cmake/cmake/issues/16456 如果只想查看header,可以使用CheckIncludeFileCXX。它只检查该头文件是否存在,但它不传递 C++11 标志 【参考方案1】:

根据documentation,可以设置CMAKE_REQUIRED_FLAGS,使try_compile使用C++11标志。

【讨论】:

谢谢,这似乎正是他们在撰写本文时在 llvm trunk 中所做的事情,特别是 CheckLibcxxAtomic.cmake 中的 libc++:llvm.org/svn/llvm-project/libcxx/trunk/cmake/Modules/…【参考方案2】:

从 CMake 3.8 开始,可以添加

cmake_policy(SET CMP0067 NEW) 

(你确实需要CMAKE_CXX_STANDARD_REQUIRED=ON)。见CMake doc on CMP0067

【讨论】:

以上是关于cmake:为啥 CMAKE_CXX_STANDARD 似乎不适用于 check_cxx_source_compiles的主要内容,如果未能解决你的问题,请参考以下文章

为啥 CMake 在 Qt Creator 中过早结束?

为啥 cmake 文件 GLOB 是邪恶的?

cmake:为啥 CMAKE_CXX_STANDARD 似乎不适用于 check_cxx_source_compiles

为啥这个 CMake 脚本找到“alloca”但仍然失败?

为啥cmake没有在makefile中添加库链接命令?

Cmake是啥?有啥作用?为啥要使用Cmake?