CMake:将(独立)库分成不同的target_link_libraries调用? [英] CMake: Splitting (independent) libraries into different target_link_libraries calls?

查看:1026
本文介绍了CMake:将(独立)库分成不同的target_link_libraries调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个目标 A 取决于库 B C 。但 B C 没有相互依赖。与CMake的链接可能类似​​于

Say I have a target A that depends on the libraries B and C. But B and C have no mutual dependence. The linking with CMake could look like

target_link_libraries( A B C )

target_link_libraries( A B )
target_link_libraries( A C )

似乎也有效(并且可能更容易维护)。将target_link_libraries命令拆分成多个命令有缺点吗?或者应该总是把它放入一个单一的命令,以最终确实遇到库的相互依赖。

also seems to work (and might be more easy to maintain). Are there disadvantages of splitting the target_link_libraries command into multiple commands? Or should one always put it into a single command in case one eventually does encounter a mutual dependence of the libraries?

推荐答案

完全等同。两个人都说A取决于B,A取决于C.既不说任何关于B和C之间的任何依赖,所以没有。

Those are exactly equivalent. Both say that A depends on B and A depends on C. Neither says anything about any dependency between B and C, so there is none.

我不知道什么你的意思是相互依赖 - 当考虑B和C,有4种可能性:(1)既不取决于另一个,(2)B取决于C,(3)C取决于B,或(4)两者依赖于彼此。

I'm not sure what you mean by "mutual dependence" -- when considering B and C, there are 4 possibilities: (1) neither depends on the other, (2) B depends on C, (3) C depends on B, or (4) they both depend on each other.

(1)是你有什么。 (2)和(3)意味着您应该添加另一个target_link_libraries命令,其中B C分别作为args或C B。 (4)意味着你有一个循环依赖,他们真的不应该是单独的库,但组合成一个单一的逻辑实体。你应该避免(4),因为它使得无法加载为某些(所有?)平台上的共享库。

(1) is what you have. (2) and (3) would mean you should add another target_link_libraries command with either "B C" as the args or "C B" respectively. (4) means you have a circular dependency and they really shouldn't be separate libraries at all, but combined into a single logical entity. You should avoid (4) because it makes it impossible to load as shared libraries on some (all?) platforms.

有两个单独的target_link_libraries调用。我怀疑你可以衡量效果,并显示任何重大的时间差异。

There is negligible performance penalty for having two separate target_link_libraries calls. I doubt you could measure the performance and show any significant timing differences.

要澄清,这个电话:

target_link_libraries(A B C)

表示目标A需要库B和C

means target A requires libraries B and C.

如果你考虑上面的情况(2),B取决于C,你可以写成:

If, instead, you consider case (2) above, where B depends on C, you would instead write:

target_link_libraries(B C)
target_link_libraries(A B)


$ b b

这意味着目标B需要库C,目标A需要库B(并且CMake自动地将B的依赖传播到A,这样你不必知道任何A→C依赖,除非你有明确的代码调用

which means target B requires library C, and target A requires library B (and CMake automatically transitively propagates B's dependencies to A so that you do not have to know about any A -> C dependence unless you have explicit code that calls functionality in library C).

您应该始终表示将事件关联在一起所需的最小依赖关系信息。

You should always express the minimum dependency information necessary to link things together.

这篇关于CMake:将(独立)库分成不同的target_link_libraries调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆