When i use CMake on my ubuntu machine (it is cmake version 3.28.3) and i download the Step4 tutorial code for the tutorial on the CMake website, changing cxx_std_11 to cxx_std_98 has no effect, but it should throw a compile error. To reduce this problem down to the bare minimum, I made my own tiny project. It has the following source code (test.cpp):
#include <iostream>
int main()
{
std::cout << std::stod(“300”) << std::endl;return 0;
}
and the following CMakeLists.txt in the same directory:
cmake_minimum_required(VERSION 3.10)
# specify the C++ standard
add_library(test_compiler_flags INTERFACE)
target_compile_features(test_compiler_flags INTERFACE cxx_std_98)# add the executable
add_executable(test test.cpp)target_link_libraries(test PUBLIC test_compiler_flags)
This does not generate a compile error but it should because im using std::stod().
Here is the version of c++ compiler im using and a demonstration that it throws an error if i manually compile it with -std=c++98:
kruno@ubuntupad:~/repos/cmake_tutorial/test$ c++ --version
c++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.kruno@ubuntupad:~/repos/cmake_tutorial/test$ c++ test.cpp
kruno@ubuntupad:~/repos/cmake_tutorial/test$ ./a.out
300
kruno@ubuntupad:~/repos/cmake_tutorial/test$ c++ test.cpp -std=c++98
test.cpp: In function ‘int main()’:
test.cpp:6:23: error: ‘stod’ is not a member of ‘std’
6 | std::cout << std::stod(“300”) << std::endl;
| ^~~~
test.cpp:6:23: note: ‘std::stod’ is only available from C++11 onwards
Also, when I export the cmake verbose commands to JSON, I do not see -std=c++98 anywhere in the compilation step. It’s like the compile features library is being ignored.
[
{
“directory”: “/home/kruno/repos/cmake_tutorial/test”,
“command”: “/usr/bin/c++ -o CMakeFiles/test.dir/test.cpp.o -c /home/kruno/repos/cmake_tutorial/test/test.cpp”,
“file”: “/home/kruno/repos/cmake_tutorial/test/test.cpp”,
“output”: “CMakeFiles/test.dir/test.cpp.o”
}
]