10

In this answer, it says Debug is the default cmake build configuration.

But I have a different observation:

I have following in my CMakeLists.txt to choose debug and release versions of a lib according to the current build configuration.

target_link_libraries(MyApp debug Widgets_d)
target_link_libraries(MyApp optimized Widgets)

It seems that when I invoke cmake without sepcifying -DCMAKE_BUILD_TYPE flag, Widgets is used instead of Widgets_d (When I delete Widgets and try to build, make complains that lib is not there). So that means by default the build configuration is optimized, not debug.

So what actually is the default build configuration? If it is debug, what could be wrong with my CMakelists.txt?

Community
  • 1
  • 1
Lahiru Chandima
  • 22,324
  • 22
  • 103
  • 179
  • http://stackoverflow.com/questions/24460486/cmake-build-type-not-being-used-in-cmakelists-txt –  Nov 24 '14 at 18:10

2 Answers2

12

If depends on whether you are using a single-configuration generator (Makefiles) or a multi-configuration generator (Visual Studio, XCode).

The link cited in the question is about a multi-configuration generator. When using a multi-configuration generator, the configuration variable CMAKE_BUILD_TYPE is ignored. To select the configuration to build, cmake allows the switch --config. In many cases, omitting --config builds a Debug configuration, but the current CMake documentation now clarifies that there is no specified default - it can even be empty.

However, when using a single-configuration generator, the switch --config is ignored. Only the configuration variable CMAKE_BUILD_TYPE is used to determine the build type. The default depends on the toolchain (see docs).

More background info on single- and multiconfiguration-generators in this answer.

Adrian W
  • 4,563
  • 11
  • 38
  • 52
  • Are you sure the default type is `Release`? I could not find this mentioned in the docs – szx May 23 '23 at 16:18
  • "The default value will often be none of the above standard configurations and will instead be an empty string. A common misunderstanding is that this is the same as Debug, but that is not the case. Users should always explicitly specify the build type instead to avoid this common problem." - https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#default-and-custom-configurations – szx May 23 '23 at 16:24
  • Thanks for the update. This is a new section in the documentation since version 3.22.6. Not sure whether this is a clarification in the documentation only or whether this is changed behavior since 3.22.6. In any case, not relying on the (assumed) default is a good advice. – Adrian W May 23 '23 at 17:21
  • @szx: about the default for `CMAKE_BUILD_TYPE`: you're right. The docs says that this is toolchain-specific. I have updated the answer. – Adrian W May 23 '23 at 17:37
10

target_link_libraries with optimized keyword corresponds to all configurations, which are not debug.

Try adding message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") to your CMakeLists.txt to see the actual build type (I suppose it should be empty).

Mikhail Maltsev
  • 1,632
  • 11
  • 21
  • So if I do not specify CMAKE_BUILD_TYPE when building, the build will be an optimized build and no debug information will be added to the binary? – Lahiru Chandima Nov 23 '14 at 09:03
  • 1
    The debug information and optimization of your own code will depend on compiler flags set for the default build type. As for the Widgets library, yes, MyApp will be linked with Widgets (not Widgets_d). If you use CMake to generate Makefiles (CMake supports other output formats as well), you could use "make VERBOSE=1" to see the exact compiler flags. – Mikhail Maltsev Nov 23 '14 at 09:36