[CMake] CMAKE_CXX_STANDARD_REQUIRED not working in helloworld

Winfried winfried_mb2 at xmsnet.nl
Tue Mar 7 09:32:27 EST 2017


I am trying to let cmake exit with an error when the c++ compiler 
doesn't support at least the C++14 standard.
According to the cmake documentation this should be possible with 
variables CMAKE_CXX_STANDARD and CMAKE_CXX_STANDARD_REQUIRED.

However when I compile the simple helloworld example below with g++ 4.8 
(which has no C++14 support), cmake supplies the option -std=c++1y to 
g++ and compilation fails.
I expected CMake to stop during configure with an error.

Can anybody see what I am doing wrong ?

Thanks in advance,
Winfried


file main.cpp :

#include <string>
#include <iostream>

using namespace std::string_literals;

int main(int, char **)
{
     std::cout << "Hello "s + "world!" << std::endl;
     return 0;
}



file CMakeLists.txt :

project(reqstd_test)
cmake_minimum_required(VERSION 3.7)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_executable(hello main.cpp)


cmake output:
wdobbe at linux-wdobbe:/data/generated/cmake_std_req/build> cmake ..
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- 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
-- Configuring done
-- Generating done
-- Build files have been written to: /data/generated/cmake_std_req/build
wdobbe at linux-wdobbe:/data/generated/cmake_std_req/build>


make output:
wdobbe at linux-wdobbe:/data/generated/cmake_std_req/build> make VERBOSE=Y
/home/wdobbe/install/cmake-3.8.0-rc2-Linux-x86_64/bin/cmake 
-H/data/generated/cmake_std_req -B/data/generated/cmake_std_req/build 
--check-build-system CMakeFiles/Makefile.cmake 0
<cut>
[ 50%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
/usr/bin/c++    -std=c++1y -o CMakeFiles/hello.dir/main.cpp.o -c 
/data/generated/cmake_std_req/main.cpp
/data/generated/cmake_std_req/main.cpp:4:22: error: ‘string_literals’ is 
not a namespace-name
  using namespace std::string_literals;
                       ^
/data/generated/cmake_std_req/main.cpp:4:37: error: expected 
namespace-name before ‘;’ token
  using namespace std::string_literals;
                                      ^
/data/generated/cmake_std_req/main.cpp: In function ‘int main(int, 
char**)’:
/data/generated/cmake_std_req/main.cpp:8:18: error: unable to find 
string literal operator ‘operator"" s’
      std::cout << "Hello "s + "world!" << std::endl;
                   ^
CMakeFiles/hello.dir/build.make:62: recipe for target 
'CMakeFiles/hello.dir/main.cpp.o' failed
make[2]: *** [CMakeFiles/hello.dir/main.cpp.o] Error 1
make[2]: Leaving directory '/data/generated/cmake_std_req/build'
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/hello.dir/all' 
failed
make[1]: *** [CMakeFiles/hello.dir/all] Error 2
make[1]: Leaving directory '/data/generated/cmake_std_req/build'
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2



More information about the CMake mailing list