Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Runtime Checks compiler flag per-project in CMAKE

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.

like image 265
Samuel Slade Avatar asked Dec 21 '11 09:12

Samuel Slade


3 Answers

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.

like image 142
Samuel Slade Avatar answered Nov 15 '22 09:11

Samuel Slade


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.

like image 30
arrowd Avatar answered Nov 15 '22 07:11

arrowd


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.

like image 2
finnan Avatar answered Nov 15 '22 08:11

finnan