What is the default build configuration of CMake?
Asked Answered
N

2

13

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 specifying -DCMAKE_BUILD_TYPE, 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?

Noemi answered 23/11, 2014 at 6:8 Comment(1)
#24460986Railway
R
11

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).

Rive answered 23/11, 2014 at 7:5 Comment(2)
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?Noemi
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.Rive
P
14

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.

Papageno answered 5/5, 2019 at 15:32 Comment(4)
Are you sure the default type is Release? I could not find this mentioned in the docsMagdalen
"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." - cmake.org/cmake/help/latest/manual/…Magdalen
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.Papageno
@szx: about the default for CMAKE_BUILD_TYPE: you're right. The docs says that this is toolchain-specific. I have updated the answer.Papageno
R
11

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).

Rive answered 23/11, 2014 at 7:5 Comment(2)
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?Noemi
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.Rive

© 2022 - 2024 — McMap. All rights reserved.