14

I have a CMAKE configuration where all my project configurations include the /RTC1 (Both Runtime Checks) compiler flag. However, I wish to switch to the Default option for only one project, as it also has the /clr compiler flag; which is incompatible with the Runtime Checks flag. I'm relatively new to CMAKE, so this may have an obvious solution, but I've so far been unable to find this.

Any help would be appreciated.

0

3 Answers 3

15

I didn't manage to find a solution whereby I could nicely remove the particular options, but I did find a way of stripping the option from the compiler flags variable using a REGEX REPLACE:

STRING (REGEX REPLACE "/RTC(su|[1su])" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")

Where this may not be the most ideal approach, it has worked well in my situation where it is a special-cased scenario.

2
  • 1
    FWIW, I had to repeat this with the CMAKE_C_FLAGS and CMAKE_C_FLAGS_DEBUG. I did this in a loop using the pattern from SDL2's CMakeLists.txt here. Aug 21, 2015 at 1:18
  • My C/C++ flags had /RTC1 and -RTC1 populated. To remove both, I used STRING (REGEX REPLACE "[/|-]RTC(su|[1su])" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
    – Kevin
    May 25, 2018 at 13:41
2

I'd recently faced with the same problem and found no elegant solution. However this code does the job:

foreach(flag_var
        CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
        CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
    STRING (REGEX REPLACE "/RTC[^ ]*" "" ${flag_var} "${${flag_var}}")
endforeach(flag_var)
set_property(TARGET necessary_targets_here APPEND_STRING PROPERTY COMPILE_FLAGS " /RTC1")

If you only need to clear /RTC flag for one configuration (ex. Debug) you may try the following approach:

STRING (REGEX REPLACE "/RTC[^ ]*" "" CMAKE_CXX_FLAGS_DEBUG  "${CMAKE_CXX_FLAGS_DEBUG}")
foreach(target_var necessary_targets_here)
  target_compile_options(${target_var} PRIVATE $<$<CONFIG:Debug>: /RTC1>)
endforeach()

Please, note using generator expression $<$<CONFIG:Debug>: /RTC1 > which expands to /RTC1 only in Debug.

1

If you are adding your flags with add_definitions(), then you can remove them with remove_definitions, see documentation.

Also, you can play with COMPILE_DEFINITIONS target property.

1
  • I tried using remove_definitions, but the original setting was set using set (...), so remove_definitions didn't work. I ended up solving the issue using a REGEX REPLACE command. Dec 21, 2011 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.