这是一个 MCVE:
\n\ncmake_minimum_required(VERSION 3.1)\nProject(Test)\n\ninclude(CheckCXXSourceCompiles)\n\nset (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")\n#set (CMAKE_CXX_STANDARD_REQUIRED TRUE)\n#set (CMAKE_CXX_STANDARD 11)\n#set (CMAKE_CXX_EXTENSIONS FALSE)\n\ncheck_cxx_source_compiles("\n#include <atomic>\n\nint main() {\n std::atomic<int> u{5};\n return u;\n}" HAVE_STDLIB_ATOMIC)\n\nif (NOT HAVE_STDLIB_ATOMIC)\n message(FATAL_ERROR "Did not find std::atomic support!")\nendif()\nRun Code Online (Sandbox Code Playgroud)\n\n当我使用该CMAKE_CXX_FLAGS版本时,它工作正常,但是当我使用我们现在应该使用的新CMAKE_CXX_STANDARD标志时,它不起作用,我收到以下构建错误:
$ cmake ..\n-- The C compiler identification is GNU 5.4.1\n-- The CXX compiler identification is GNU 5.4.1\n-- Check for working C compiler: /usr/bin/cc\n-- Check for working C compiler: /usr/bin/cc -- works\n-- Detecting C compiler ABI info\n-- Detecting C compiler ABI info - done\n-- Detecting C compile features\n-- Detecting C compile features - done\n-- Check for working CXX compiler: /usr/bin/c++\n-- Check for working CXX compiler: /usr/bin/c++ -- works\n-- Detecting CXX compiler ABI info\n-- Detecting CXX compiler ABI info - done\n-- Detecting CXX compile features\n-- Detecting CXX compile features - done\n-- Performing Test HAVE_STDLIB_ATOMIC\n-- Performing Test HAVE_STDLIB_ATOMIC - Failed\nCMake Error at CMakeLists.txt:20 (message):\n Did not find std::atomic support!\n\n\n-- Configuring incomplete, errors occurred!\nSee also "/home/chris/cmake_test/build/CMakeFiles/CMakeOutput.log".\nSee also "/home/chris/cmake_test/build/CMakeFiles/CMakeError.log".\nRun Code Online (Sandbox Code Playgroud)\n\n错误日志表明它没有使用该-std=c++11标志:
$ cat /home/chris/cmake_test/build/CMakeFiles/CMakeError.log \nPerforming C++ SOURCE FILE Test HAVE_STDLIB_ATOMIC failed with the following output:\nChange Dir: /home/chris/cmake_test/build/CMakeFiles/CMakeTmp\n\nRun Build Command:"/usr/bin/make" "cmTC_42a05/fast"\n/usr/bin/make -f CMakeFiles/cmTC_42a05.dir/build.make CMakeFiles/cmTC_42a05.dir/build\nmake[1]: Entering directory \'/home/chris/cmake_test/build/CMakeFiles/CMakeTmp\'\nBuilding CXX object CMakeFiles/cmTC_42a05.dir/src.cxx.o\n/usr/bin/c++ -DHAVE_STDLIB_ATOMIC -o CMakeFiles/cmTC_42a05.dir/src.cxx.o -c /home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx\nIn file included from /usr/include/c++/5/atomic:38:0,\n from /home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx:2:\n/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.\n #error This file requires compiler and library support \\\n ^\n/home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx: In function \xe2\x80\x98int main()\xe2\x80\x99:\n/home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx:5:3: error: \xe2\x80\x98atomic\xe2\x80\x99 is not a member of \xe2\x80\x98std\xe2\x80\x99\n std::atomic<int> u{5};\n ^\n/home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx:5:15: error: expected primary-expression before \xe2\x80\x98int\xe2\x80\x99\n std::atomic<int> u{5};\n ^\n/home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx:6:10: error: \xe2\x80\x98u\xe2\x80\x99 was not declared in this scope\n return u;\n ^\nCMakeFiles/cmTC_42a05.dir/build.make:65: recipe for target \'CMakeFiles/cmTC_42a05.dir/src.cxx.o\' failed\nmake[1]: *** [CMakeFiles/cmTC_42a05.dir/src.cxx.o] Error 1\nmake[1]: Leaving directory \'/home/chris/cmake_test/build/CMakeFiles/CMakeTmp\'\nMakefile:126: recipe for target \'cmTC_42a05/fast\' failed\nmake: *** [cmTC_42a05/fast] Error 2\n\nSource file was:\n\n#include <atomic>\n\nint main() {\n std::atomic<int> u{5};\n return u;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我的cmake版本是:
$ cmake --version\ncmake version 3.5.1\n\nCMake suite maintained and supported by Kitware (kitware.com/cmake).\nRun Code Online (Sandbox Code Playgroud)\n\n我不明白为什么这里cmake不使用该std=c++11标志,根据文档,CMAKE_CXX_STANDARD应该从版本 3.1 开始工作。
有人知道这里出了什么问题吗?
\n\n最合适的解决方法是什么?我应该CMAKE_CXX_FLAGS对测试和CMAKE_CXX_STANDARD目标进行调整吗?在我看来,测试和目标应该使用完全相同的配置,否则测试的意义何在:/
| 归档时间: |
|
| 查看次数: |
5713 次 |
| 最近记录: |