I would like to clarify one detail here that nobody has mentioned yet.
This point especially applies when cross compiling, but is valid otherwise too.
CMAKE_HOST_WIN32
is the variable that is set when compiling ON Windows.
WIN32
is the variable that is set when compiling FOR a Windows target platform.
So CMAKE_HOST_WIN32
is the correct flag to use considering OP's question literally "when I am building on Windows". In many cases WIN32
and CMAKE_HOST_WIN32
will be equivalent, but not in all cases.
WIN32
will be set too in the beginning when cross-compiling ON Windows FOR non-Windows, but will be implicitly unset at some point during CMake execution (I believe inside project
call). Last time I checked WIN32
was set during executing toolchain.cmake, but was not set at a later point. So in these cases this flag can be dangerous as its state changes during the execution. You should NOT use WIN32
inside a toolchain file!
If you need details about the platform you are building ON, you can try variables CMAKE_HOST_SYSTEM_NAME
and CMAKE_HOST_SYSTEM_VERSION
MESSAGE("CMAKE_HOST_SYSTEM_NAME ${CMAKE_HOST_SYSTEM_NAME}")
MESSAGE("CMAKE_HOST_SYSTEM_VERSION ${CMAKE_HOST_SYSTEM_VERSION}")
gives me the following output (on CMake version 3.19.2)
CMAKE_HOST_SYSTEM_NAME Windows
CMAKE_HOST_SYSTEM_VERSION 10.0.19044
There is also CMAKE_HOST_SYSTEM
which inside toolchain file gave me blank, but after project
call it gave "Windows-10.0.19044"