set(CMAKE_CXX_STANDARD) adding 2 more commands, is that necessary ?

Hi!
I can set a latest C++ standard with
set(CMAKE_CXX_STANDARD 23) or add_compile_options(“/std:c++latest”)
I set verbose mode to see what it’s doing under the hood and
In case of set(CMAKE_CXX_STANDARD 23) CMake build from Visual Studio launches 4 commands
[1/4] is cl.exe with compile args
[2/4] is cmake itself with -E cmake_ninja_dyndep and other commands
[3/4] is another launch of cl.exe (for some reason)
[4/4] and then another launch of cmake

while if setting latest standard like that in cmakelists file:
add_compile_options(“/std:c++latest”)
it launches only 2 commands cl.exe and cmake.
or I can say it’s not double launching them , why such behavior ? Is that a bug/issue ?

Windows 11
Visual Studio
CMake 3.28

In the former, CMake knows that C++20 or newer is in effect and is performing module scanning commands. The 1/4 is a scanning command to extract import and export metadata needed for a proper build; 3/4 is the actual compile. When passing options to the compiler “behind CMake’s back” like add_compile_options does, CMake doesn’t know that modules are available and doesn’t perform the necessary steps.

1 Like