[CMake] CMAKE_BUILD_TYPE and exact control of compiler options

Dan Liew dan at su-root.co.uk
Tue Oct 13 06:10:01 EDT 2015


Hi,


> - If not, what is the best/official way to get exact control over the compiler
> and linker options used?

I had to do something similar recently where I didn't want
``-DNDEBUG`` to be in any of the configurations.

I used  ``CMAKE_USER_MAKE_RULES_OVERRIDE `` to set the path to file
containing overrides, this must be set before calling ``project()``

```
set(CMAKE_USER_MAKE_RULES_OVERRIDE
${CMAKE_CURRENT_SOURCE_DIR}/cmake/c_flags_override.cmake)
project(ABC C)
```

The contents of ``c_flags_override.cmake`` looks like this

```
if (("${CMAKE_C_COMPILER_ID}" MATCHES "Clang") OR
("${CMAKE_C_COMPILER_ID}" MATCHES "GNU"))
# Taken from Modules/Compiler/GNU.cmake but -DNDEBUG is removed
  set(CMAKE_C_FLAGS_INIT "")
  set(CMAKE_C_FLAGS_DEBUG_INIT "-g")
  set(CMAKE_C_FLAGS_MINSIZEREL_INIT "-Os")
  set(CMAKE_C_FLAGS_RELEASE_INIT "-O3")
  set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "-O2 -g")
else()
  message(FATAL_ERROR "Overrides not set for compiler ${CMAKE_C_COMPILER_ID}")
endif()
```

I'm not sure if this is really the correct route to go down though for
your use case.

Dan.


More information about the CMake mailing list