本文介绍了使用cmake跨不同目录的项目依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的项目结构:

Suppose I have such project structure:

Root
  + ProjA
     + SubA.1
     + SubA.2
  + ProjB
     + SubB.1
     + SubB.2

我设置的依赖项是:

SubA.2 depends on SubA.1
ProjA depends on SubA.1 and SubA.2
ProjB depends on ProjA

使以 make -jX 开头的构建顺序如下所示:

I want to make the build order looks like this when start with make -jX:

1. SubA.1
2. SubA.2
3. SubB.1 SubB.2

但是事实是:

1. SubA.1 SubB.1 SubB.2
2. SubA.2

ProjB =>在 SubA 之后,不能使用ProjA 来构建所有 SubB

It seems the ProjB => ProjA can't be used to make all the SubBs build after SubAs.

在所有 SubA SubB

How can I make all the SubBs build after all the SubAs finishes?

推荐答案

在CMake中,依赖关系只能按目标设置。

In CMake dependencies can be set only on per-target basis.

您应该确定 ProjB 是否取决于 ProjA 内部 c $ c>与否。 内部是指目标名称,CMake变量的值等等。

You should decide, whether ProjB depends on internals of ProjA or not. By "internals" I mean names of targets, values of CMake variables and so on.


  1. 如果 ProjB 实际上需要 ProjA 的内部组件,您可以仔细确定 ProjA 的目标或 ProjB 的另一个目标,并仅设置所需的依赖项。这样构建 ProjA 可能会与 ProjB make -j ,但构建是正确的。

  1. If ProjB actually require internals of ProjA, you may carefully determine targets of ProjA, which are required for one or another target of ProjB, and set only needed dependencies. Such a way building of ProjA may interleave with ProjB with make -j, but building will be correct.

例如,如果 SubB.1 是可执行文件,需要 SubA.1 库进行链接,然后

E.g., if SubB.1 is executable, which requires SubA.1 library for link with, then

target_link_libraries(SubB.1 SubA.1)

自动设置所需的依赖项。

automatically sets needed dependencies.

如果使用可执行文件 SubA.2 生成了 SubB.2 ,则用法

If SubB.2 is generated using executable SubA.2, then usage

add_custom_command(OUTPUT <subB.2_file>
    COMMAND SubA.2 <args>
)

将自动设置所需的依赖项。

will automatically set needed dependencies.


  1. 如果 ProjB 不知道 ProjA 的内部,并且只有在已经构建 ProjA 的情况下才能构建 ProjB ,然后您可以使用 ExternalProject_Add 用于构建两个项目并设置已创建目标之间的依赖关系:

  1. If ProjB doesn't aware about internal of ProjA and it is only known that ProjB can be built only when ProjA is already built, then you may use ExternalProject_Add for build both projects and set dependencies between created targets:

CMakeLists.txt

ExternalProject_Add(ProjA ...)
ExternalProject_Add(ProjB ...)
# Now `ProjA` and `ProjB` are targets in top-level project.
add_dependencies(ProjB ProjA)

这篇关于使用cmake跨不同目录的项目依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:15