James Bennet
James Bennet

Reputation: 603

CMAKE - Debug / Crossbuild?

Ok im using CMake to build C++ under Linux, for an ARM target. I have a #DEFINE in one of my .h files called DEBUG. If set, I would like for various methods for tracing over serial will be added into the build. I would like to be able to do this by doing e.g. "make debug" to build with this #DEFINE set, and have normal "make" build without setting it. Is this possible?

Also, it it possible to be able to specify a target to CMake? As I have 2 CMakeLists.txt right now, one for x86, and one for Armel (with different options such as to build with debug information on x86, compared to the ARM, which wants a stripped, size-optimised binary).

Upvotes: 3

Views: 3245

Answers (3)

Yantao Xie
Yantao Xie

Reputation: 12896

It seems you have two questions. One about debug, you can use CMAKE_BUILD_TYPE to separate your debug/release setting, and use $ cmake --build . --config Release or $ cmake --build . --config Debug to compile.

For the second, about cross build, it's possible. You can do like these:

  1. According to this guide write configure files for arm and x86 respectively to set compiler, archiver, etc.. Suppose name the one of x86 as *config_x86.cmake* and *config_arm.cmake* for arm.
  2. Define a cmake macro, such as *MY_TARGET*. And add the following code before the project command,

    if (${MY_TARGET} STREQUAL "x86")
        include(config_x86.cmake REQUIRED)
    elseif (${MY_TARGET} STREQUAL "arm")
        include(config_arm.cmake REQUIRED)
    endif ()
    
  3. Then generate your project files like this

    $ cmake -DMY_TARGET=x86 "your code dir"

Upvotes: 2

srking
srking

Reputation: 4732

Regarding definitions, you can do what you want like this in your CMakeLists.txt files:

if( CMAKE_BUILD_TYPE MATCHES "[Dd][Ee][Bb][Uu][Gg]" )
    message( "INFO: DEBUG BUILD" )
    # for debug type builds, turn on verbose makefiles
    SET(CMAKE_VERBOSE_MAKEFILE ON)

    # Tell other CMake files that we're doing a debug build
    SET( DEBUG_BUILD 1 )

    # Tell C/C++ that we're doing a debug build
    ADD_DEFINITIONS( -DDEBUG )
endif()

if( CMAKE_BUILD_TYPE MATCHES "[Rr][Ee][Ll][Ee][Aa][Ss][Ee]" )
    message( "INFO: RELEASE BUILD" )
endif()

CMake does not like supporting two targets, in other words two toolchains, in the same build tree. There's really no need for this anyway. Definitely use out-of-tree builds with one build tree for each of your possible targets. Something like this:

cd /path/to/x86_build
cmake /path/to/src/x86/CMakeLists.txt
cd /path/to/Armel_build
cmake /path/to/src/Armel/CMakeLists.txt

Upvotes: 1

Rian Sanderson
Rian Sanderson

Reputation: 6702

Debug and release builds are supported out of the box by CMake.

Say the top level CMakeLists.txt for your project is in ~/project/foobar, you can create separate debug and a release builds by doing this:

mkdir ~/project/build

mkdir ~/project/build/foobar-debug 
cd ~/project/build/foobar-debug 
cmake -DCMAKE_BUILD_TYPE:STRING=Debug ~/project/foobar 

mkdir ~/project/build/foobar-rel
cd ~/project/build/foobar-rel
cmake -DCMAKE_BUILD_TYPE:STRING=Release ~/project/foobar 

What's even better is that CMake supports not just the standard debug/release targets you're probably used to, but it supports minimum sized release, as well as release with debug info. See the other options for CMAKE_BUILD_TYPE at: http://cmake.org/Wiki/CMake_Useful_Variables

Upvotes: 1

Related Questions