Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Cmake generate a single makefile that supports both debug and release

Tags:

c++

c

cmake

It seems like we need to create separate folder for each build type (debug/release), run cmake on each, and generate separate makefile for debug/release configuration. Is it possible to create one single makefile using cmake that supports both debug/release configuration at the same time and when we actually run "make" that will create separate folders for the intermediate and final products (like the dlls, exe).

like image 875
user243655 Avatar asked Apr 10 '12 05:04

user243655


People also ask

Is CMake and Makefile the same?

Make (or rather a Makefile) is a buildsystem - it drives the compiler and other build tools to build your code. CMake is a generator of buildsystems. It can produce Makefiles, it can produce Ninja build files, it can produce KDEvelop or Xcode projects, it can produce Visual Studio solutions.

What does CMake generate?

CMake can generate a native build environment that will compile source code, create libraries, generate wrappers and build executables in arbitrary combinations. CMake supports in-place and out-of-place builds, and can therefore support multiple builds from a single source tree.

How do I enable debug symbols in CMake?

compile in Release mode optimized but adding debug symbols, useful for profiling : cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ... or compile with NO optimization and adding debug symbols : cmake -DCMAKE_BUILD_TYPE=Debug ...

What is MinSizeRel?

MinSizeRel is the same as Release, with its optimization configuration just set to Minimize Size instead of Maximize Speed as illustrated in this link in Visual Studio, for example.


2 Answers

As far as I know, this cannot be achieved using a single set of build scripts. However, what you can do is have two sub-directories of your working area:

build/
build/debug
build/release

Then do:

$ cd build
$
$ cd build/debug
$ cmake -DCMAKE_BUILD_TYPE=Debug ../..
$ make
$
$ cd ../release
$ cmake -DCMAKE_BUILD_TYPE=Release ../..
$ make

If necessary, you can add another build script in the build directory as such:

#!/bin/sh
cd debug   && make && cd ..
cd release && make && cd ..
like image 106
André Caron Avatar answered Sep 20 '22 08:09

André Caron


This can be achieved using the ADD_CUSTOM_TARGET command. For example, if you want to add both debug and release targets in your makefile, add the following to your CMakeLists.txt file:

ADD_CUSTOM_TARGET(debug
  COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug ${CMAKE_SOURCE_DIR}
  COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
  COMMENT "Creating the executable in the debug mode.")

ADD_CUSTOM_TARGET(release
  COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Release ${CMAKE_SOURCE_DIR}
  COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
  COMMENT "Creating the executable in the release mode.")

Then, after configuring with cmake, you can run make debug to make the debug target and run make release to make the release target in the same directory.

like image 31
a.sam Avatar answered Sep 21 '22 08:09

a.sam