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

强颜欢笑 提交于 2020-01-22 10:50:13

问题


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).


回答1:


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 ..



回答2:


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.



来源:https://stackoverflow.com/questions/10083427/can-cmake-generate-a-single-makefile-that-supports-both-debug-and-release

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!