使用cmake编译组

tno*_*rgd 5 c++ cmake

我有一个项目,其中编译产生许多可执行文件.我使用cmake生成Makefile.然后当我说make,所有这些都被编译.我知道我可能会make target1用来编译一个理想的目标.但是我想将我的所有目标分成小组并且能够使用,比如make group_A编译目标的子集.怎么做到这一点?

该项目是用C++编写的,并在Linux和OSX下开发.

Lak*_*arg 5

查看CMake文档add_custom_target并查看add_dependencies.您可以将组作为自定义目标添加到要构建的目标作为组的依赖项.

http://www.cmake.org/cmake/help/v3.2/command/add_custom_target.html http://www.cmake.org/cmake/help/v3.2/command/add_dependencies.html

编辑(在@ms的评论之后)

你可以做

add_custom_target(<group-name> DEPENDS <target1> <target2> ...)
Run Code Online (Sandbox Code Playgroud)

这是一个小例子

hello1.cpp

#include <stdio.h>

int main() {
    printf("Hello World 1\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

hello2.cpp

#include <stdio.h>

int main() {
    printf("Hello World 2\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

hello3.cpp

#include <stdio.h>

int main() {
    printf("Hello World 3\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

的CMakeLists.txt

cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(groups_demo)

add_executable(hello1 hello1.cpp)
add_executable(hello2 hello2.cpp)
add_executable(hello3 hello3.cpp)

add_custom_target(hello12 DEPENDS hello1 hello2)
add_custom_target(hello23 DEPENDS hello3 hello2)
add_custom_target(hello13 DEPENDS hello1 hello3)
Run Code Online (Sandbox Code Playgroud)

您现在可以使用make hello12构建hello1hello2

  • 你甚至不需要`add_dependency`,只需使用:`add_custom_target(group1 DEPENDS target1 target2)` (2认同)