Optimization flags hardcoded in CMakeLists.txt

Issue #317 new
Stefan Ritt created an issue

Just now I spent two hours trying to debug a program where variables where out of scope, until I realized that CMakeLists.txt had the optimization flags hard-coded with

add_compile_options("-O2")

which is an absolute no-go in the CMake environment. CMake has different compile models (“Debug, Release, RelWithDebugInfo”) and we can define flags for each of these models, but never ever put them directly into CMakeLists.txt. See here for details:

https://stackoverflow.com/questions/41361631/optimize-in-cmake-by-default

Users want to be able to switch from debugging (no optimizations) to release mode (our ‘normal’ optimization, typically “-g -O 2”) without having to change CMakeLists.txt each time.

I know a command like

cmake .. -DCMAKE_BUILD_TYPE=Debug or

cmake .. -DCMAKE_BUILD_TYPE=RelWithDebInfo

might be hard to remember, but if that’s too complicated put it into a script (even if the script is called ‘Makefile’).

I request that the option

add_compile_options("-O2")

gets removed from CMakeLists.txt once and forever.

Stefan

Comments (2)

  1. Log in to comment