[CMake] how to use CMAKE_CXX_FLAGS_DEBUG and CMAKE_CXX_FLAGS_RELEASE ?

Brad King brad.king at kitware.com
Sun Feb 19 15:41:32 EST 2006


Alexander Neundorf wrote:
> Hi, 
>  
> CMAKE_CXX_FLAGS_RELEASE and CMAKE_CXX_FLAGS_DEBUG are appended to 
> CMAKE_CXX_FLAGS if the buildtype is release or debug. 
>  
> Now let's say I'd like to have the following flags: 
> default: -Wall -fno-rtti -O2 -g  
> release: -Wall -fno-rtti -O2 
> debug:   -Wall -fno-rtti -O0 -g 
>  
> I can't set CMAKE_CXX_FLAGS to "-Wall -fno-rtti -O2" because the -O2 will 
> conflict with the -O0 for the debug build. 
> I would have expected either this behaviour: 
>  
> SET(CMAKE_CXX_FLAGS_RELEASE "-Wall -fno-rtti -O2") 
> SET(CMAKE_CXX_FLAGS_DEBUG  "-Wall -fno-rtti -O0 -g") 
> SET(CMAKE_CXX_FLAGS "-Wall -fno-rtti -O2 -g" )  
>  
> or: 
> SET(CMAKE_CXX_FLAGS_DEFAULT "-O2 -g") 
> SET(CMAKE_CXX_FLAGS_RELEASE "-O2") 
> SET(CMAKE_CXX_FLAGS_DEBUG  "-O0 -g") 
> SET(CMAKE_CXX_FLAGS "-Wall -fno-rtti" )  
>  
> But the special FLAGS are always appended to the general FLAGS and 
> (AFAIK) there is no such variable like CMAKE_CXX_FLAGS_DEFAULT. 
>  
> So, how is it intended to be used ? 

The default would never be used on a multi-configuration generator such 
as the Visual Studio or Xcode 2.x generators.  In a single-configuration 
generator the configuration is selected by CMAKE_BUILD_TYPE.  The 
problem is that this variable may be empty, but "no configuration" does 
not really make sense (but we've been living with it).  You could make 
it an error in your CMakeLists.txt file for CMAKE_BUILD_TYPE to be left 
empty, or just provide your own default setting.  The "default" options 
you are providing with "-O2 -g" is basically the "release with debug 
info" configuration we call RELWITHDEBINFO.  I suggest something like

# Set a default build type for single-configuration
# CMake generators if no build type is set.
IF(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
   SET(CMAKE_BUILD_TYPE RelWithDebInfo)
ENDIF(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)

# Select flags.
SET(CMAKE_CXX_FLAGS "-Wall -fno-rtti")
SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
SET(CMAKE_CXX_FLAGS_RELEASE "-O2")
SET(CMAKE_CXX_FLAGS_DEBUG  "-O0 -g")

-Brad


More information about the CMake mailing list