mardi 29 novembre 2016

cmake: why doesn't CMAKE_CXX_STANDARD seem to work here?

Here's an MCVE:

Project(Test)
cmake_minimum_required(VERSION 3.1)

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()

When I use the CMAKE_CXX_FLAGS version, it works fine, but when I use the new CMAKE_CXX_STANDARD flags which we are supposed to use now, it doesn't work, I get the following build errors:

$ 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".

The error log indicates it's not using the -std=c++11 flag:

$ 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;
}

My cmake version is:

$ cmake --version
cmake version 3.5.1

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

I can't figure out why cmake doesn't use the std=c++11 flag here, according to the docu, CMAKE_CXX_STANDARD is supposed to work since version 3.1.

Anyone know what's wrong here?

Aucun commentaire:

Enregistrer un commentaire