CMake tutorial step4 code has no effect when setting cxx_std_98

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”
}
]

Ok, i just read into the documentation a bit more and cxx_std_98 doest mean it will add the -std=c++98 flag, it simply means that it will require that the compiler AT LEAST support that standard. Here is some more info from the docs:

So im curious then, what would be the way in modern cmake to specifically specify u want the flag -std=c++98? I guess if cmake has a way to specify compiler flags u could use that method. Probabaly as i work through the tutorial, this question will be answered.

does it help? Determining the compiler default C++ standard - #13 by adaldev

1 Like

Yes thanks!

I have a followup question to this reply: Can you also just manually specify that you want to use the compiler argument “-std=c++98”? Or would that be a very non-CMake thing to do because you take away the abstraction of being compiler agnostic?