[CMake] Release flags on one target in debug build type

Dan Liew dan at su-root.co.uk
Fri May 5 19:34:32 EDT 2017


On 5 May 2017 at 19:45, Konstantin Tokarev <annulen at yandex.ru> wrote:
> Hello,
>
> Is there any clear way to build specific target in "Debug" mode with flags that it would have in "Release"?
>
> In particular, build this specific target without effect of CMAKE_CXX_FLAGS_DEBUG and CMAKE_C_FLAGS_DEBUG, while preserving flags added by target_compile_options


Something like this would sort of do it.

```
target_compile_options(your_target
  PRIVATE
  $<$<COMPILE_LANGUAGE:CXX>:$<$<CONFIG:Debug>:${CMAKE_CXX_FLAGS_RELEASE}>>
  $<$<COMPILE_LANGUAGE:C>:$<$<CONFIG:Debug>:${CMAKE_C_FLAGS_RELEASE}>>
```

The problem you have though is that `your_target` in debug mode will
already debug build flags (e.g. `-O0 g`). So you may get conflicting
compiler options.

Another way of doing this is to build your target as an external
project where you set the build mode of the external project to be
release.


More information about the CMake mailing list