How to change the build type to Release mode in cmake?

I am trying to build a project in Release mode. By default it is built in debug mode. I am setting the variable CMAKE_BUILD_TYPE to "Release" in CMakeLists.txt. But it is still building the project in debug mode. When I pass "Release" as the build type in the CMake command, it still does not work.

The CMake command that I am using is:

cmake -G"Visual Studio 10" -DCMAKE_BUILD_TYPE=Release
-H"source_path" -B"Build path"

Please provide a solution if any.

104615 次浏览

You cannot set the default build type for Visual Studio from the command line.

CMake's Visual Studio Generators will generate the four standard profiles (Debug, RelWithDebInfo, MinSizeRel and Release) and you have to choose the one you want to build from within VS. This is because the information about the active configuration is not part of the project files generated by CMake, but part of the .suo file generated by VS.

If you want an automated build of a particular configuration, use MSBuild instead of VS which allows you to specify a configuration on the command line.

Use it as you do it but in the root cmake file add the following before the project keyword

SET(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} CACHE STRING "" FORCE)
PROJECT(MY_PROJECT)#It's here just to show where you should add it.

To change the build type, on Windows, it must be done at build time:

cmake --build {DIR} --config Release

By default it's Debug. I'm still looking for a way of changing this default. CMAKE_BUILD_TYPE doesn't work of course, and tweaking CMAKE_CONFIGURATION_TYPES doesn't work either, obviously for the same reason, they only apply for Unix makefiles, not for Visual projects.

I checked it with Visual Studio 2015 and cmake 3.3 .

Short answer

Link

cmake --build {BUILD_DIR_PATH} --target ALL_BUILD --config {BUILD_TYPE}

Example

cmake --build . --target ALL_BUILD --config Release

Long answer

cmake -G{GENERATOR_NAME} -B{BUILD_DIR_PATH} -H{SOURCE_DIR_PATH}


cmake --build {BUILD_DIR_PATH} --target ALL_BUILD --config {BUILD_TYPE}

Example

cmake -GVisual Studio 14 -Bbuild/win32/x86 -H.


cmake --build build/win32/x86 --target ALL_BUILD --config Release

Additional info

  • "-G" - specifies the generator name

  • "-B" - specifies path to the build folder

  • "-H" - specifies path to the source folder

Tried the things below which worked for me to build the binaries in release/debug mode in Windows.

Added the following line in the root CMakeLists.txt file, just above the project command:

SET(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} CACHE STRING "" FORCE)

Used the following command for setting the release mode configuration:

cmake -DCMAKE_BUILD_TYPE=Release ..

Used this command to build the same in Release mode:

cmake --build . --config Release

You can repeat the same procedure for debug mode as well, it works.

Bit late, but I found this worked for me and was quite clean: This means that just calling cmake builds in release mode, but if you want debug mode just call cmake -DCMAKE_BUILD_TYPE=Debug

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE)
endif()

If your generator is a single-config generator like "Unix Makefiles" or "Ninja", then the build type is specified by the CMAKE_BUILD_TYPE variable, which can be set in the configure command by using -DCMAKE_BUILD_TYPE:STRING=Release.

For multi-config generators like the Visual Studio generators and "Ninja Multi-Config", the config to build is specified in the build command using the --config argument argument like --config Release. A default value can currently be specified at configure time for the Ninja Multi-Config generator by setting the value of the CMAKE_DEFAULT_BUILD_TYPE variable, which will be used if the --config argument isn't passed to the build command. At the current time, a default cannot be set for Visual Studio generators.