c++ - cmake:为什么 CMAKE_CXX_STANDARD 似乎不能与 check_cxx_source_compiles 一起使用

标签 c++ c++11 cmake

这是一个 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> u{5};
  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> u{5};
   ^
/home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx:5:15: error: expected primary-expression before ‘int’
   std::atomic<int> u{5};
               ^
/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> u{5};
  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 吗?在我看来,测试和目标应该使用完全相同的配置,否则测试的意义何在:/

最佳答案

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

关于c++ - cmake:为什么 CMAKE_CXX_STANDARD 似乎不能与 check_cxx_source_compiles 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40877744/

相关文章:

c++ - 没有这样的文件或目录,在 fstream c++ 中被杀死

c++ - Qt QString::split() 的相反方法

c++ - AWS S3 aws-sdk-cpp GetObject "Unable to connect to endpoint"

c++ - 为什么这些文件打不开?

C++11 外部模板 : where do we actually need them?

c++ - 无法在 std::map<std::string,std::shared_ptr<class>> 中设置值

c++ - 尝试构建示例代码时出现编译错误

C++(11) : When to use direct or copy initialization if both are perfectly fine

c++ - 使用 Google Test 编译程序时出现 "g++ is not a full path"

android-studio - 将 Android Studio 指向特定的 cmake 版本