问题描述
假设我有这样的项目结构:
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 SubB
s build after SubA
s.
在所有 SubA $ A之后,如何构建所有
SubB
How can I make all the SubB
s build after all the SubA
s 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.
- 如果
ProjB
实际上需要ProjA
的内部组件,您可以仔细确定ProjA
的目标或ProjB
的另一个目标,并仅设置所需的依赖项。这样构建ProjA
可能会与ProjB
和make -j $ c交错$ c>,但构建是正确的。
- If
ProjB
actually require internals ofProjA
, you may carefully determine targets ofProjA
, which are required for one or another target ofProjB
, and set only needed dependencies. Such a way building ofProjA
may interleave withProjB
withmake -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.
- 如果
ProjB
不知道ProjA
的内部,并且只有在已经构建ProjA
的情况下才能构建ProjB
,然后您可以使用ExternalProject_Add
用于构建两个项目并设置已创建目标之间的依赖关系:
- If
ProjB
doesn't aware about internal ofProjA
and it is only known thatProjB
can be built only whenProjA
is already built, then you may useExternalProject_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跨不同目录的项目依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!