96

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.

Nathan Osman
  • 71,149
  • 71
  • 256
  • 361
123r789
  • 1,600
  • 3
  • 22
  • 33
  • try using `cmake -DCMAKE_BUILD_TYPE=Release -H"soruce_path" -B"Buidl path" -G"Visual Studio 10" ` – bikram990 Sep 26 '13 at 09:34
  • 1
    It is giving the same result. It is Building the project in Debug mode only. – 123r789 Sep 26 '13 at 09:51
  • 1
    CMAKE_CONFIGURATION_TYPES Specifies the available build types. This specifies what build types will be available such as Debug, Release, RelWithDebInfo etc. This has reasonable defaults on most platforms. But can be extended to provide other build types. See also CMAKE_BUILD_TYPE. – bikram990 Sep 26 '13 at 09:53
  • 1
    CMAKE_BUILD_TYPE Specifies the build type for make based generators. This specifies what build type will be built in this tree. Possible values are empty, Debug, Release, RelWithDebInfo and Min- SizeRel. This variable is only supported for make based generators. If this variable is supported, then CMake will also provide initial values for the variables with the name CMAKE_C_FLAGS_[Debug|Release|RelWithDebInfo|MinSizeRel]. For example, if CMAKE_BUILD_TYPE is Debug, then CMAKE_C_FLAGS_DEBUG will be added to the CMAKE_C_FLAGS. – bikram990 Sep 26 '13 at 09:54
  • you can play around with these two options if either of them works – bikram990 Sep 26 '13 at 09:54

8 Answers8

237

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.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
Rok
  • 2,550
  • 1
  • 14
  • 5
  • 2
    You are not changing configuration build type, you are building Release configuration of project. Though the answer is right, +1 –  Dec 06 '13 at 16:44
  • 1
    `CMAKE_CONFIGURATION_TYPES doesn't work either` [CMAKE_CONFIGURATION_TYPES](http://www.cmake.org/cmake/help/v2.8.12/cmake.html#variable:CMAKE_CONFIGURATION_TYPES) variable controls build configurations for current project (take a look at `solution configurations` of visual studio project). See comments above. –  Dec 06 '13 at 16:54
  • 6
    This works for me. Additionally, when I want to do an install I do `cmake --build . --config Release --target install`. Then cmake works it's magic! – PfunnyGuy Nov 17 '17 at 03:26
  • @PfunnyGuy that’s super helpful, tho I wonder why `cmake --install .` doesn’t work the same way with the VS generator. – Frederik Aug 06 '21 at 20:57
54

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

Maks
  • 2,808
  • 4
  • 30
  • 31
  • 1
    --config Release doesn't work for me. Only -DCMAKE_CONFIGURATION_TYPES=Release works. – user5280911 Jun 18 '18 at 10:02
  • 1
    `ALL_BUILD` affects all targets that are depending on the project. I would prefer not touching it. What if you are working on your project but want to build its dependencies in release mode? This is a perfectly find and common example. – rbaleksandar Sep 21 '21 at 12:18
26

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.

Smi
  • 13,850
  • 9
  • 56
  • 64
ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
  • 10
    Then what is the use of CMAKE_BUILD_TYPE. – 123r789 Sep 26 '13 at 11:02
  • 9
    @123r789 It is used for example for Makefile generators which only allow one profile at the time. IDE generators like VS generate multiple profiles at once and allow choosing one from the IDE. With Makefiles you only ever have one profile so the choice must be made at CMake configure time. The [documentation](http://www.cmake.org/cmake/help/v2.8.11/cmake.html#variable:CMAKE_BUILD_TYPE) also points this out explicitly: _This variable [CMAKE_BUILD_TYPE] is only supported for make based generators._ – ComicSansMS Sep 26 '13 at 11:09
  • It is simply not true. Cmake is pretty flexible of what could be changed in the default behavior. – ixSci Sep 26 '13 at 13:38
  • 5
    @ixSci Your solution does not change the default, it _completely removes_ any configurations besides the Release. I would argue that's a bit harsh for this kind of problem. I also disagree with the -1 (not implying that it was you) as the central argument of my answer is still valid: _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._ – ComicSansMS Sep 26 '13 at 15:33
  • Yes, just as you don't need to switch build type frequently it is pretty convenient to have only one type. It is easy to change to the another one by cmake console command. – ixSci Sep 26 '13 at 16:36
  • 4
    I switch build type frequently. I can't see how it's convenient to only have one type either - you're removing useful functionality. This is the correct answer to the OP's question IMHO. – Fraser Sep 26 '13 at 17:19
  • @ComicSansMS +1, but I think it's good to mention [bikram990](http://stackoverflow.com/users/1578528/bikram990)'s note that profiles can be controlled by [CMAKE_CONFIGURATION_TYPES](http://www.cmake.org/cmake/help/v2.8.12/cmake.html#variable:CMAKE_CONFIGURATION_TYPES) –  Dec 06 '13 at 16:39
  • I was trying to build with `msbuild.exe` after CMake produced the project files (generated from Makefile), and thanks to this comment I searched further to reconfigure the msbuild.exe itself, this one works for me at this moment: `msbuild project_file_generated_by_cmake.vcxproj /property:Configuration=Release` – Ped7g Aug 15 '19 at 03:19
  • While not complete answer to the specific context , +1 for the fundo explanation .. – Mohammad Kanan Mar 21 '21 at 00:54
5

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.

Louis Langholtz
  • 2,913
  • 3
  • 17
  • 40
  • Note: `CMAKE_BUILD_TYPE` is normally only for single-config generators. `--config` is only for multi-config generators. Setting `CMAKE_CONFIGURATION_TYPES` will cause CMake with a multi-config generator to only generate the build type specified by `CMAKE_BUILD_TYPE`, which kind of defeats the purpose of using a multi-config generator- unless the generator doesn't have a single-config variant like Visual Studio. And why not use the `CMAKE_DEFAULT_BUILD_TYPE` option? – starball Mar 02 '23 at 00:54
2

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. Since CMake 3.17, 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.

starball
  • 20,030
  • 7
  • 43
  • 238
1

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()
aalpatya
  • 19
  • 2
0

If you want to generate solution file with default build type=Release, use -DCMAKE_CONFIGURATION_TYPES=Release just after -B<build_dir> option.

Example:

cmake -H. -G"Visual Studio 17 2022" -B<build_dir> -DCMAKE_CONFIGURATION_TYPES=Release

If you have generated solution files with default build type=Debug and want to build it using cmake --build use --config Release just after --build option

Example:

cmake --build <build_dir> --config Release

This worked for me with cmake version 3.26.3

-21

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.
ixSci
  • 13,100
  • 5
  • 45
  • 79
  • 6
    I think this would be a very unexpected way to go about setting the build type for those used to Visual Studio. While it *does* work, I certainly wouldn't *recommend* this method. – Fraser Sep 26 '13 at 17:15
  • 1
    @Fraser, any arguments? – ixSci Sep 26 '13 at 17:18
  • 8
    My main argument is that this is an unusual technique. If it's used in a private project where nobody except the author is expected to have to run CMake, then cool. If I had to use this, it would be surprising initially that there is only one build type. Once I saw the reason, it would become slightly annoying, since you're not actually trying to disable other build types; you're just forcing multi-config users to have to work as if they were single-config users. – Fraser Sep 26 '13 at 17:27
  • 1
    Who decides what is usual and what is not? For me it is a usual technique. I use it on everyday basis. And developers work on debug most of the time. At least it is my experience. So switching is rare. And I don't see any problem or annoyance to have run cmake with -DCMAKE_BUILD_TYPE= once you need to switch your configuration. And yes it disables other build types and not forcing anyone to anything. It does exactly that: leave only one build type. – ixSci Sep 26 '13 at 17:33
  • 6
    I'm not *deciding* it's usual - it's an observation having seen the way that many other projects work. I can point to several projects which don't do this. I don't know of a single one which does. And for me, it's a lot easier to do 2 clicks in my IDE to change build types than open a command prompt, navigate to my build root, then type `cmake . -DCMAKE_BUILD_TYPE=Release`. Neither is hard, but I'd choose the easier way every time. – Fraser Sep 26 '13 at 18:47
  • 9
    Let's not forget you are relying on undocumented behavior here. CMake does only guarantee that CMAKE_BUILD_TYPE does the right thing [for make based generators](http://www.cmake.org/cmake/help/v2.8.11/cmake.html#variable:CMAKE_BUILD_TYPE). Your solution might suddenly stop working with any future CMake version. – ComicSansMS Sep 27 '13 at 07:21