How to use cmake, how to define default compiler flags for each type of assembly?

I am trying to configure my build system so that my release builds do not include additional debugging. But I can not find anywhere in the CMake documentation that lists which compiler flags are used by default for the type of assembly, and I would prefer not to invent my own way of doing something.

Does this feature already exist? Without resorting to trial and error, how can I determine which flags are used by default for different types of assemblies?

+6
source share
1 answer

This blog post contains some useful information, and this post describes some common anti-patterns.

The four types of builds that CMake includes are Release , Debug , RelWithDebInfo and MinSizeRel . Accordingly, CMake sends default values ​​for each specific assembly type to CMAKE_C_FLAGS_<buildType> and CMAKE_CXX_FLAGS_<buildType> .

If you want to find out what are the default values ​​for each type of assembly, you can add the following instructions to your CMakeLists.txt :

 message("CMAKE_C_FLAGS_DEBUG is ${CMAKE_C_FLAGS_DEBUG}") message("CMAKE_C_FLAGS_RELEASE is ${CMAKE_C_FLAGS_RELEASE}") message("CMAKE_C_FLAGS_RELWITHDEBINFO is ${CMAKE_C_FLAGS_RELWITHDEBINFO}") message("CMAKE_C_FLAGS_MINSIZEREL is ${CMAKE_C_FLAGS_MINSIZEREL}") message("CMAKE_CXX_FLAGS_DEBUG is ${CMAKE_CXX_FLAGS_DEBUG}") message("CMAKE_CXX_FLAGS_RELEASE is ${CMAKE_CXX_FLAGS_RELEASE}") message("CMAKE_CXX_FLAGS_RELWITHDEBINFO is ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}") message("CMAKE_CXX_FLAGS_MINSIZEREL is ${CMAKE_CXX_FLAGS_MINSIZEREL}") 

In my version of cmake (cmake version 2.8.12.1 on OS X), this prints the following values:

 CMAKE_C_FLAGS_DEBUG is -g CMAKE_C_FLAGS_RELEASE is -O3 -DNDEBUG CMAKE_C_FLAGS_RELWITHDEBINFO is -O2 -g -DNDEBUG CMAKE_C_FLAGS_MINSIZEREL is -Os -DNDEBUG CMAKE_CXX_FLAGS_DEBUG is -g CMAKE_CXX_FLAGS_RELEASE is -O3 -DNDEBUG CMAKE_CXX_FLAGS_RELWITHDEBINFO is -O2 -g -DNDEBUG CMAKE_CXX_FLAGS_MINSIZEREL is -Os -DNDEBUG 

(As you can see, flag sets are the same by default for C and C ++.)

Be careful because the default assembly type is an empty string. Therefore, if you do not specify the type of assembly, none of the above applies. The following code was suggested on the cmake mailing list (should be placed around the top of the top level CMakeLists.txt , I think) for those who do not want this behavior:

 if (NOT CMAKE_BUILD_TYPE) message(STATUS "No build type selected, default to Release") set(CMAKE_BUILD_TYPE "Release") endif() 

However, this is not recommended because it will break some mutli configuration generators. (Better to install it inside the build script.)

(One blog post above talked about using a shell alias to set -DCMAKE_BUILD_TYPE=Debug when invoked instead of cmake .)

+10
source

Source: https://habr.com/ru/post/972266/


All Articles