2016-11-29 87 views
1

這裏工作是一個MCVE:cmake的:爲什麼不CMAKE_CXX_STANDARD似乎與check_cxx_source_compiles

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?在我看來,是完全相同的配置應該用於測試和目標,否則什麼是測試點:/

+0

注:我發現了這個問題,目前正在追蹤他們的問題跟蹤,但如果你能說明一個很好的修訂或變通方法,我會接受的答案https://gitlab.kitware.com/cmake/cmake/issues/16456 –

+0

如果你只想檢查標題,你可以使用'CheckIncludeFileCXX'。它只檢查該頭是否存在,它不傳遞C++ 11標誌,儘管 – Danh

回答

2

documentation,您可以設置CMAKE_REQUIRED_FLAGS使try_compile使用C++ 11的標誌。

+1

謝謝,這看起來正是他們在編寫本文時在llvm主幹中所做的,特別是針對'CheckLibcxxAtomic.cmake'中的libC++:https ://llvm.org/svn/llvm-project/libcxx/trunk/cmake/Modules/CheckLibcxxAtomic.cmake –

相關問題