使用 cmake 编译组

compilation groups with cmake

本文关键字:编译 cmake 使用      更新时间:2023-10-16

我有一个项目,其中编译会产生许多可执行文件。我使用 cmake 来生成生成文件。然后当我说make,它们都被编译了。我知道我可以使用make target1来编译所需的目标。但是我想将所有目标分成几组,并能够使用make group_A来编译目标的子集。如何实现这一点?

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

请查看 CMake 文档中的add_custom_targetadd_dependencies。可以将组添加为自定义目标,并将要构建的目标作为组的依赖项。

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

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

你可以做

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

这里有一个小例子

你好1.cpp

#include <stdio.h>
int main() {
    printf("Hello World 1n");
    return 0;
}

你好2.cpp

#include <stdio.h>
int main() {
    printf("Hello World 2n");
    return 0;
}

你好3.cpp

#include <stdio.h>
int main() {
    printf("Hello World 3n");
    return 0;
}

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