catkin_make is just a wrapper around CMake, so you can use the standard CMake variable CMAKE_BUILD_TYPE
to enable various build options.
For a release configuration with opmizations and no debugging symbols, you can use the Release
build type:
catkin_make -DCMAKE_BUILD_TYPE=Release
For a release with debug configuration, with optimizations and debug symbols, you can use theRelWithDebInfo
build type:
catkin_make -DCMAKE_BUILD_TYPE=RelWithDebInfo
- //========================================================================
- CMAKE_BUILD_TYPE
- A variable which controls the type of build when using a single-configuration generator like the Makefile generator. CMake will create by default the following variables when using a single-configuration generator:
- None (CMAKE_C_FLAGS or CMAKE_CXX_FLAGS used)
- Debug (CMAKE_C_FLAGS_DEBUG or CMAKE_CXX_FLAGS_DEBUG)
- Release (CMAKE_C_FLAGS_RELEASE or CMAKE_CXX_FLAGS_RELEASE)
- RelWithDebInfo (CMAKE_C_FLAGS_RELWITHDEBINFO or CMAKE_CXX_FLAGS_RELWITHDEBINFO
- MinSizeRel (CMAKE_C_FLAGS_MINSIZEREL or CMAKE_CXX_FLAGS_MINSIZEREL)
You can use these default compilation flags (or modify them) by setting the CMAKE_BUILD_TYPE variable at configuration time from within the "ccmake" GUI.
Note! The default values for these flags change with different compilers. If CMake does not know your compiler, the contents will be empty.
If you are using the Makefile generator, you can create your own build type like this:
SET(CMAKE_BUILD_TYPE distribution)
SET(CMAKE_CXX_FLAGS_DISTRIBUTION "-O3")
SET(CMAKE_C_FLAGS_DISTRIBUTION "-O3")
Note that CMAKE_BUILD_TYPE is not initialized with a readable value at configuration time. This is because the user is free to select a build type at build time. Use CMAKE_CFG_INTDIR if you need a variable that evaluates to the correct build time directory.
//========================================================================
It's generally best to do an "out of source" build. Create your CMakeLists.txt
in the root of your project. Then from the root of your project:
mkdir Release
cd Release
cmake -DCMAKE_BUILD_TYPE=Release ..
make
And for Debug (again from the root of your project):
mkdir Debug
cd Debug
cmake -DCMAKE_BUILD_TYPE=Debug ..
make
Debug
will add the debug flags appropriate for your compiler. See
this FAQ for more details.
You can modify/add to the flags in your CMakeLists.txt
via CMAKE_C_FLAGS_DEBUG
and CMAKE_C_FLAGS_RELEASE
variables, e.g.:
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall")
See
this link under
CMAKE_BUILD_TYPE
for more details.
//========================================================================
> 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")
//========================================================================
print out the value of building variables:
message("CMAKE_BUILD_TYPE is ${CMAKE_BUILD_TYPE}")
message("CMAKE_CXX_FLAGS is ${CMAKE_CXX_FLAGS}")
message("CMAKE_CXX_FLAGS_DEBUG is ${CMAKE_CXX_FLAGS_DEBUG}")
message("CMAKE_CXX_FLAGS_RELEASE is ${CMAKE_CXX_FLAGS_RELEASE}")
message("CMAKE_CXX_FLAGS_MINSIZEREL is ${CMAKE_CXX_FLAGS_MINSIZEREL}")
message("CMAKE_CXX_FLAGS_RELWITHDEBINFO is ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")