[Cmake] Adding compile flags in several steps

Brad King brad.king at kitware.com
Tue Aug 17 08:35:07 EDT 2004


Martin Magnusson wrote:
> I'd like to specify a set of compiler flags which should always be used, 
> and another set which should be appended under certain circumstances. I 
> tried doing it like this:
> 
> SET_SOURCE_FILES_PROPERTIES(
>   ${SOURCES}
>   COMPILE_FLAGS "-Wall -Werror")
> 
> IF(DEBUG_MODE)
>   SET_SOURCE_FILES_PROPERTIES(
>     ${SOURCES}
>     COMPILE_FLAGS "-g -O0 -DDEBUG_MODE=1" )
>   MESSAGE("Debug build.")
> ELSE(DEBUG_MODE)
>   SET_SOURCE_FILES_PROPERTIES(
>     ${SOURCES}
>     COMPILE_FLAGS "-O3 -DDEBUG_MODE=0" )
>   MESSAGE("Release build.")        
> ENDIF(DEBUG_MODE)
> 
> but then, the "-Wall -Werror" is always overwritten. What's the correct 
> way to do this?

You can use GET_SOURCE_FILE_PROPERTY to obtain the previous value of the 
COMPILE_FLAGS property and then append the flags and use 
SET_SOURCE_FILES_PROPERTIES to set the new value.  In your case, though, 
you should just set the flags in the CMAKE_CXX_FLAGS_DEBUG and 
CMAKE_CXX_FLAGS_RELEASE variables.  Flags in these variables are 
automatically included in release or debug builds.

If DEBUG_MODE is actually an option available to the user in the CMake 
cache to enable/disable some extra debugging code then you should not 
add it to the build this way.  When the user changes it you want the 
project to rebuild properly.  You should use CONFIGURE_FILE to configure 
a header file with this setting recorded.  When it changes the file will 
be updated and any dependent sources will rebuild.

-Brad


More information about the Cmake mailing list