1

给定以下 CMakeLists.txt 和一些任意的 hello.c:

cmake_minimum_required(VERSION 2.6)

get_filename_component(debug_rpath ${CMAKE_CURRENT_BINARY_DIR}/hello-debug.exe REALPATH)
message(STATUS "debug exe: ${debug_rpath}")
get_filename_component(release_rpath ${CMAKE_CURRENT_BINARY_DIR}/hello-release.exe REALPATH)
message(STATUS "release exe: ${release_rpath}")

add_custom_command(
  OUTPUT hello-release.exe
  COMMAND clang ${CMAKE_SOURCE_DIR}/hello.c -O3 -o ${release_rpath}
  MAIN_DEPENDENCY hello.c
#  DEPENDS hello.c
  IMPLICIT_DEPENDS hello.c
  COMMENT "Compiling release..."
  VERBATIM)

add_custom_command(
  OUTPUT ${debug_rpath}
  COMMAND clang ${CMAKE_SOURCE_DIR}/hello.c -g -DNDEBUG -o ${debug_rpath}
  MAIN_DEPENDENCY hello.c
#  DEPENDS hello.c
  IMPLICIT_DEPENDS hello.c
  COMMENT "Compiling debug..."
  VERBATIM)


add_custom_target(hello-release ALL
  DEPENDS ${release_rpath}
  SOURCES hello.c
  VERBATIM)


add_custom_target(hello-debug ALL
  DEPENDS ${debug_rpath}
  SOURCES hello.c
  VERBATIM)

(debug/release 和 clang 作为编译器就是例子,我知道 cmake 有它自己的机制。重要的细节是我有两个使用相同主输入文件的自定义命令)

执行hello-debug目标输出:

> mingw32-make hello-debug
[ 50%] Compiling release...
[100%] Compiling debug...
[100%] Built target hello-debug

为什么它还要构建发布?

取消注释SOURCESorMAIN_DEPENDENCY选项只会构建选定的目标。我了解这两个选项仅影响 Visual Studio 项目生成的文档。我同时使用“MinGW Makefiles”和“Visual Studio”生成器,因此保留 SOURCES 选项实际上会很好。错误还是我在监督什么?

4

0 回答 0