Issue
I was trying to set the default build type to Release
with CMake, using the method described here (straight from the horse's mouth). This method checks if CMAKE_BUILD_TYPE
is already set, and sets it to Release
only if its value was empty. This works fine on macOS and Linux. However, on Windows, CMAKE_BUILD_TYPE
is already set to Debug
from the default. Why does this happen and how can I work around it, so that I can reliably set the default build type to Release
?
To try this out yourself, create a trivial CMake file:
# CMakeLists.txt
project(Foo)
message(STATUS "Build type is '${CMAKE_BUILD_TYPE}'")
Then use with with a single-configuration generator, such as cmake . -G"NMake Makefiles"
or cmake . -GNinja
. In both cases, on Windows I see: Build type is 'Debug'
. On other platforms I see Build type is ''
.
Solution
CMAKE_BUILD_TYPE
is getting set on the project
line. Before that it is empty. Thus, checking whether CMAKE_BUILD_TYPE
is empty and setting a default value can be done before this line.
Here is a discussion about this: https://gitlab.kitware.com/cmake/cmake/-/issues/19401
Answered By - Szabolcs