1

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.

bremen_matt
  • 6,902
  • 7
  • 42
  • 90

1 Answers1

4

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.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    I understand that the flags will be compiler specific. I really wish there was an online resource to inspect. – bremen_matt Mar 21 '19 at 07:49
  • Dumb question... Why doesn't Clang have such flags? – bremen_matt Mar 21 '19 at 08:14
  • If I search through all of the cmake files for `FLAGS_RELEASE_INIT`, then for almost every compiler (Gray, Intel, Nvidia, SunPro, Arm, etc) there is such a line, but not for Clang. – bremen_matt Mar 21 '19 at 08:16
  • It has the same, it includes GNU, from [here](https://github.com/Kitware/CMake/blob/master/Modules/Compiler/Clang.cmake#L19) – KamilCuk Mar 21 '19 at 08:17
  • I think the fastest way would be to setup a small project and printing out the `CMAKE_C_FLAGS_*_INIT` values, that way you would be 100% sure what they are. – KamilCuk Mar 21 '19 at 08:18
  • That seems sneaky and bad to just include the GNU cmake file. Thanks for the pointer – bremen_matt Mar 21 '19 at 08:19