gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:36:03 25 4
gpt4 key购买 nike

这是一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com