3

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.

1 Answer 1

6

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.

6
  • 2
    I understand that the flags will be compiler specific. I really wish there was an online resource to inspect. Commented Mar 21, 2019 at 7:49
  • Dumb question... Why doesn't Clang have such flags? Commented Mar 21, 2019 at 8: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. Commented Mar 21, 2019 at 8:16
  • It has the same, it includes GNU, from here
    – KamilCuk
    Commented Mar 21, 2019 at 8: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
    Commented Mar 21, 2019 at 8:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.