[CMake] Default CMAKE_C_FLAGS value by OS

ycollette.nospam at free.fr ycollette.nospam at free.fr
Thu Feb 16 04:34:12 EST 2017


cmake version on fedora 25: 3.6.2

And the corresponding lines in GNU.cmake (there are some differences):

  # Initial configuration flags.
  set(CMAKE_${lang}_FLAGS_INIT "")
  set(CMAKE_${lang}_FLAGS_DEBUG_INIT "-g")
  set(CMAKE_${lang}_FLAGS_MINSIZEREL_INIT "-Os -DNDEBUG")
  set(CMAKE_${lang}_FLAGS_RELEASE_INIT "-O2 -DNDEBUG")
  set(CMAKE_${lang}_FLAGS_RELWITHDEBINFO_INIT "-O2 -g -DNDEBUG")
  set(CMAKE_${lang}_CREATE_PREPROCESSED_SOURCE "<CMAKE_${lang}_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -E <SOURCE> > <PREPROCESSED_SOURCE>")
  set(CMAKE_${lang}_CREATE_ASSEMBLY_SOURCE "<CMAKE_${lang}_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -S <SOURCE> -o <ASSEMBLY_SOURCE>")

So, as you can see, on  Fedora 25, there are no -O3 flags by default.

YC

----- Mail original -----
De: "Dan Liew" <dan at su-root.co.uk>
À: "ycollette nospam" <ycollette.nospam at free.fr>
Cc: "cmake" <cmake at cmake.org>
Envoyé: Jeudi 16 Février 2017 10:26:01
Objet: Re: [CMake] Default CMAKE_C_FLAGS value by OS

Hi,

On 16 February 2017 at 09:06,  <ycollette.nospam at free.fr> wrote:
> Hello,
>
> My question is related to CMAKE_*_FLAGS.
> I've got a project under linux fedora 24 and, in release mode, this project compiles with the -O2 flag.
> But when I switched to other platform (ubuntu, fedora 16 - I now this one is quite old but I need to compile on this platform), this default optimization flag changes. On some platform, it's -O3.
> And because with -O3 flag some "risky" optimizations are enabled, my project hangs ...

You can find CMake's defaults for gcc/clang in

/usr/share/cmake-<version>/Modules/Compiler/GNU.cmake

where <version> is your CMake version.

E.g.

https://github.com/Kitware/CMake/blob/master/Modules/Compiler/GNU.cmake

You'll see lines like

```
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")
```

It's possible that the defaults changed at some point but I would be
really surprised by this. Are you sure you weren't doing a
RELWITHDEBINFO build on one system and a RELEASE build on the other?

It is possible to override these defaults by writing an overrides file
(example [1]) and then using those overrides before the `project()`
declaration in your root `CMakeLists.txt` file.
Here's an example [2].

So if you need fine grained control over the optimization level used
for different build types then I suggest you use overrides.


[1] https://github.com/delcypher/fp-bench/blob/ebc8f939500b6ec28e6530f65273df8bfb122970/cmake/c_flags_override.cmake
[2] https://github.com/delcypher/fp-bench/blob/ebc8f939500b6ec28e6530f65273df8bfb122970/CMakeLists.txt#L5

HTH,
Dan.


More information about the CMake mailing list