cmake default build type differs for --build vs --install for the Visual Studio generator
Originally discussed in this forum thread: https://discourse.cmake.org/t/why-is-the-default-for-build-different-than-install
CMake version: 3.28.1
I have a CMakeLists.txt file that contains a custom target used to exercise installation for testing purposes. That target looks like:
add_custom_target(
test-install
COMMAND
${CMAKE_COMMAND}
--build ${CMAKE_BINARY_DIR}
COMMAND
${CMAKE_COMMAND}
--install ${CMAKE_BINARY_DIR}
--prefix install
This works as expected on a Linux system with the "Unix Makefiles" generator. The following commands suffice to configure the project and perform an installation to a local directory.
$ cmake -B build -S .
...
$ cmake --build build --target test-install
...
However, on Windows with the "Visual Studio 17 2022" generator, these steps fail because the build step defaults to performing a "Debug" build and the install step attempts to install a "Release" build:
>cmake -B build -S .
-- Building for: Visual Studio 17 2022
...
>cmake --build build --target test-install
...
-- Install configuration: "Release"
...
>dir /o build\x64
...
12/29/2023 08:13 PM <DIR> .
12/29/2023 08:13 PM <DIR> ..
12/29/2023 08:13 PM <DIR> Debug
0 File(s) 0 bytes
3 Dir(s) 324,411,035,648 bytes free
This seems an odd inconsistency and it isn't obvious (at least to me) how to change the custom target and still allow the build type to be controlled via the CMAKE_BUILD_TYPE
and CMAKE_CONFIGURATION_TYPES
variables.
I don't know if this inconsistent build type default issue exists for other multi-configuration generators.