I would like to remove a set compile flag for a single translation unit. Is there a way to do this? (e.g. using set_property?)
Note: the compile-flag ha
If there is no compile flag negation option (which would be the standard way of CMake handling this for non-IDE configurators), you have to remove this compile flag from CMAKE_CXX_FLAGS.
Make COMPILE_FLAGS a source file property which is INHERITED from a directory property with the same name. Now you can modify COMPILE_FLAGS property of each directory and source file individually:
# Remove '-fname' from global flags
string(REPLACE "-fname" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
define_property(
SOURCE
PROPERTY COMPILE_FLAGS
INHERITED
BRIEF_DOCS "brief-doc"
FULL_DOCS "full-doc"
)
# Add '-fname' for directory
set_directory_properties(PROPERTIES COMPILE_FLAGS "-fname")
add_executable(${PROJECT_NAME} "main.cpp")
# Remove '-fname' from source file
set_source_files_properties("main.cpp" PROPERTIES COMPILE_FLAGS "")
If some property - like COMPILE_OPTIONS - is only available on target level, you have to move the files you want to have different settings for into a separate new target itself.
This can be done by using OBJECT libraries:
# Remove '-fname' from global flags
string(REPLACE "-fname" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
# Set '-fname' for directory, but if you also use add_compile_options() be aware that
# this will overwrite your previous setting. Append/remove properties instead.
set_directory_properties(
PROPERTIES
COMPILE_OPTIONS "-fname"
)
# Remove '-fname' from source file(s) in separate object library target
add_library(${PROJECT_NAME}_no_name OBJECT "main.cpp")
set_target_properties(
${PROJECT_NAME}_no_name
PROPERTIES
COMPILE_OPTIONS ""
)
# Use object library target in e.g. executable target
add_executable(${PROJECT_NAME} $)