Equivalent of INTERFACE for set_target_properties

add_library(config INTERFACE)

set_target_properties(config
        PROPERTIES
        COMPILE_WARNING_AS_ERROR ON
        LINK_WARNING_AS_ERROR ON
)

add_executable(exec PRIVATE main.cpp)
target_link_libraries(exec PRIVATE config)

This doesn’t work as expected because the properties on config are not propagated to exec. What would be the right way to do this?

Those are not properties you can or should try to force on consumers. A consuming project may have different objectives to you and may not want to make warnings be treated as errors. Outside of a company environment where things are tightly controlled, you shouldn’t even turn these on for the project itself. It should be under the control of the developer to decide whether they want to turn warnings into errors. They might want to try building with a newer compiler that has more warnings than earlier versions, so they may need to allow warnings for a while as they work through addressing them.

CMake presets are a much better place for a project to specify turning warnings into errors. The developer can still override settings specified there, but they can be good pre-canned setups for things like CI or common developer environments.

2 Likes