6

What's the recommended way of overriding optimization level flags (-O2, -O3 etc. and the MSVC-counterparts with /)?

target_compile_options(mytarget PUBLIC "$<$<CONFIG:RELWITHDEBINFO>:/Od>")

This results in the compiler output:

cl : Command line warning D9025 : overriding '/O2' with '/Od'

I am happy to use the latest CMake, 3.13.2.

Ela782
  • 5,041
  • 5
  • 53
  • 66
  • 3
    There isn't a good way. Someone was asking this on the CMake mailing list and there was some talk about how / why these flags were picked in the first place. You can however try setting `CMAKE__FLAGS__INIT` or parsing `CMAKE__FLAGS_` and replacing `/O2` with `/Od`. Or you can manually update the cmake script files that set these values in your installation. – fdk1342 Dec 17 '18 at 19:38
  • 1
    @Fred Hmm that's a bummer! Do you happen to have the link to that mailing list thread? And since `CMAKE_CXX_FLAGS_RELWITHDEBINFO` would already contain `/O2`, which needs to be removed before `/Od` can be added - without touching any of the other compiler flags in the variable - what would be the best way to replace/substitute that specific flag? – Ela782 Dec 18 '18 at 01:32
  • When I use `target_compile_options(mytarget PUBLIC "$<$:/Od>")` I get no warnings and it works as expected. The `/O2` is not shown in the command line properties for the target. – fdk1342 Dec 19 '18 at 16:16
  • 3
    Anyways `string(REPLACE "/O2" "/Od" CMAKE_CXX_FLAGS_RELWITHDEBINFO ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})` replaces the flag. – fdk1342 Dec 19 '18 at 16:20
  • For anyone looking how to override the default init-Values: https://stackoverflow.com/a/32956785/20213170 was very helpful for me. – nick May 23 '23 at 13:31

0 Answers0