Remove compiler check flag for each project in CMAKE

I have a CMAKE configuration where all my project configurations include the /RTC1 (Both Runtime Checks) compiler flag. However, I want to switch to the Default parameter for only one project, since it also has the /clr flag of the compiler; which is incompatible with the check flag. I'm relatively new to CMAKE, so this may have an obvious solution, but I still haven't been able to find it.

Any help would be appreciated.

+7
source share
3 answers

I was not able to find a solution due to which I could easily remove certain parameters, but I found a way to remove the option from the compiler flags variable using REGEX REPLACE:

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

If this may not be the most ideal approach, it worked well in my situation, where it is a scenario with special scenarios.

+10
source

If you add your flags with add_definitions() , you can remove them with remove_definitions , see the documentation .

Alternatively, you can play with the COMPILE_DEFINITIONS target property.

+2
source

I recently ran into the same problem and did not find an 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 the /RTC flag for one configuration (e.g. debugging), you can 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 the expression $<$<CONFIG:Debug>: /RTC1 > , which expands to /RTC1 only in Debug.

+1
source

All Articles