bremen_matt
bremen_matt

Reputation: 7339

Cmake build types (RELEASE, DEBUG, etc.) compiler flags

I would like some clarity regarding cmake build types.

Specifically, it isn't clear to me whether setting a build type will also modify the build flags, or whether this is just a "label" that is used internally for the build configuration. For example, in the case of a release build:

set(CMAKE_BUILD_TYPE Release)

will the O3 flag automatically be specified to the compiler? or do I need to explicitly specify it?

One answer I found sets both the build type and explicitly sets the compiler flags:

Optimize in CMake by default

But another thread I found online suggests that there are defaults:

https://cmake.org/pipermail/cmake/2016-May/063379.html

If the build type does specify some compiler flags, where can I find documentation for that? I would like to know what flags each build type is setting.

EDIT:

For future reference, if you want to look for the specific flags for your compiler (e.g. gnu in the case of gcc or g++), then you can clone the repo that Kamil references, go into the modules/compilers folder and try a command like:

grep -r _INIT . | grep -i gnu

In fact, as Kamil points out, these flags will also be the same as the ones used by Clang since the Clang cmake file includes the GNU one.

Upvotes: 3

Views: 4791

Answers (1)

KamilCuk
KamilCuk

Reputation: 140880

The flag depends on the compiler. The -O3 flag is something understood by gcc, but may be not understood by other compilers. You can inspect the files inside Modules/Compilers/* of your cmake installation to see what flags are added depending on configuration.

For example in GNU.cmake we can read:

  string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
  string(APPEND CMAKE_${lang}_FLAGS_DEBUG_INIT " -g")
  string(APPEND CMAKE_${lang}_FLAGS_MINSIZEREL_INIT " -Os -DNDEBUG")
  string(APPEND CMAKE_${lang}_FLAGS_RELEASE_INIT " -O3 -DNDEBUG")
  string(APPEND CMAKE_${lang}_FLAGS_RELWITHDEBINFO_INIT " -O2 -g -DNDEBUG")

I don't thing you will find "documentation" for that.

Upvotes: 6

Related Questions