使用cmake编译组

时间:2015-06-18 15:01:47

标签: c++ cmake

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

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

1 个答案:

答案 0 :(得分:5)

在CMake文档中查看add_custom_targetadd_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

编辑(在@ m.s。评论之后)

你可以做到

add_custom_target(<group-name> DEPENDS <target1> <target2> ...)

这是一个小例子

hello1.cpp

#include <stdio.h>

int main() {
    printf("Hello World 1\n");
    return 0;
}

hello2.cpp

#include <stdio.h>

int main() {
    printf("Hello World 2\n");
    return 0;
}

hello3.cpp

#include <stdio.h>

int main() {
    printf("Hello World 3\n");
    return 0;
}

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

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