给出以下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)
(作为编译器的调试/发布和clang是示例,我知道cmake有自己的机制。重要的细节是我有两个使用相同主输入文件的自定义命令)
执行hello-debug
目标输出:
> mingw32-make hello-debug
[ 50%] Compiling release...
[100%] Compiling debug...
[100%] Built target hello-debug
为什么它还会构建发布版?
取消注释SOURCES
或MAIN_DEPENDENCY
选项只构建所选目标。我理解像这两个选项的文档只会影响Visual Studio项目的生成。我使用" MinGW Makefiles"和#34; Visual Studio"生成器同时,因此保持SOURCES选项实际上是好的。我还在监视某些错误?