Rather that using the method presented here, I would like to manually set a property of a Visual Studio project in the CMake file, in this case, the Platform Toolset to $(DefaultPlatformToolset)
(see this image), which is a VS macro. Is it possible?

- 1
- 1

- 229
- 3
- 8
1 Answers
This is generally what CMAKE_GENERATOR_TOOLSET
is used for, however, it should be used within a toolchain file, not within the CMakeLists.txt.
Native build system toolset name specified by user.
Some CMake generators support a toolset name to be given to the native build system to choose a compiler. If the user specifies a toolset name (e.g. via the cmake -T option) the value will be available in this variable.
The value of this variable should never be modified by project code. A toolchain file specified by the CMAKE_TOOLCHAIN_FILE variable may initialize CMAKE_GENERATOR_TOOLSET. Once a given build tree has been initialized with a particular value for this variable, changing the value has undefined behavior.
I think if you set it before making your project
call, then it will theoretically still work, although obviously this is not the supported method of using it, so compatibility may vary.

- 9,741
- 2
- 37
- 78
-
I can confirm that it works to define `CMAKE_GENERATOR_TOOLSET` before the `project` call. In my case, setting v120_xp from VS2017. – Neal Kruis Dec 06 '17 at 20:45
-
Sorry for the delay. Simply doing `set(CMAKE_GENERATOR_TOOLSET "$(DefaultPlatformToolset)")` in a toolchain file worked like a charm. Thanks! – haidahaida Apr 24 '21 at 11:24