Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using cmake, how do you determine the default compiler flags per build type?

Tags:

I'm trying to configure my build system so that my release builds don't have extra debugging included. But I can't find anywhere in the CMake documentation that lists which compiler flags are used by default per build type, and I'd rather not invent my own way of doing things.

Does this functionality already exist? Without resorting to trial and error, how can I determine what flags are used by default for the various build types?

like image 638
mpontillo Avatar asked Jul 15 '14 20:07

mpontillo


People also ask

What is the default build type in CMake?

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

How does CMake know which compiler to use?

CMake needs a way to determine which compiler to use to invoke the linker. This is determined by the LANGUAGE property of source files of the target , and in the case of static libraries, the LANGUAGE of the dependent libraries.


1 Answers

This blog post has some helpful information, and this post describes some common anti-patterns.

The four build types that CMake includes out of the box are Release, Debug, RelWithDebInfo, and MinSizeRel. Correspondingly, CMake ships with default values for each defined build type in CMAKE_C_FLAGS_<buildType> and CMAKE_CXX_FLAGS_<buildType>.

If you want to find out the what the default values are for each build type, you can add the following statements 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}")

On 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, the sets of flags are the same for C and C++ by default.)

Be cautious, because the default build type is an empty string. So unless you specify a build type, none of the above applies. On the cmake mailing list, the following code was suggested (should be placed near the top of a top-level CMakeLists.txt, I imagine) for those who do not desire 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 set it inside a build script.)

(One of the blog posts above advocated using a shell alias to set -DCMAKE_BUILD_TYPE=Debug when cmake is first invoked instead.)

like image 65
mpontillo Avatar answered Oct 08 '22 19:10

mpontillo