[CMake] Default CMAKE_C_FLAGS value by OS
Dan Liew
dan at su-root.co.uk
Thu Feb 16 04:26:01 EST 2017
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