C++ 共享库的 CMake 和订单相关链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12204820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:01:35  来源:igfitidea点击:

CMake and order dependent linking of shared libraries

c++cmakedynamic-linking

提问by dusktreader

I have a few small components that I am building as shared libraries for my main application. Lets use an example of libaand libb. Each is built within their own subdirectory as follows:

我有一些小组件正在构建为我的主应用程序的共享库。让我们以liba和为例libb。每个都构建在自己的子目录中,如下所示:

add_library(liba SHARED a.cpp)

Then, in the root project folder, I need to link my main application to both.

然后,在根项目文件夹中,我需要将我的主应用程序链接到两者。

include_directories(a)
include_directories(b)
add_executable(dummy dummy.cpp)
target_link_libraries(dummy a b)

CMake runs fine with this, and my application compiles but fails to link. The problem is that b references a. If I supply the order of the libraries while linking as

CMake 运行良好,我的应用程序编译但无法链接。问题是 b 引用了 a。如果我在链接时提供库的顺序

target_link_libraries(dummy b a)

The program compiles and links just fine

该程序编译和链接就好了

When this sort of system starts involving more complex inter dependency of the libraries, it starts to be impossible even if the dependencies are acyclic. How do I manage the linking step here? Is there a trick to ordering libraries for linking in CMake?

当这种系统开始涉及更复杂的库相互依赖时,即使依赖是非循环的,它也开始变得不可能。我如何管理此处的链接步骤?在 CMake 中订购用于链接的库有什么技巧吗?

采纳答案by Fraser

You can specify the relationship between aand bby adding

您可以指定之间的关系a,并b通过添加

target_link_libraries(b a)


From the docs:


文档

Library dependencies are transitive by default. When this target is linked into another target then the libraries linked to this target will appear on the link line for the other target too.

默认情况下,库依赖项是可传递的。当这个目标链接到另一个目标时,链接到这个目标的库也会出现在另一个目标的链接线上。

So, if you specify aas a dependency of bin this way, you don't even need to explicitly list ain any target which depends on b, i.e. your other command can be just:

因此,如果您以这种方式指定a为依赖项b,您甚至不需要明确列出a依赖于 的任何目标b,即您的其他命令可以只是:

target_link_libraries(dummy b)

although it wouldn't do any harm to list aas well.

虽然列出也不会造成任何伤害a

回答by sonicwave

An easy solution (especially for circular dependencies) can be to just put all your libraries in a list variable, then add that list twice (or more if necessary), like:

一个简单的解决方案(特别是对于循环依赖)可以是将所有库放在一个列表变量中,然后添加该列表两次(或更多,如果需要),例如:

set(LINK_LIBS "liba libb libc")
target_link_libraries(app ${LINK_LIBS} ${LINK_LIBS})

(or just type out the list twice after each other in the target_link_librariesfunction)

(或者只是在target_link_libraries函数中依次输入两次列表)

This has worked for me quite a couple of times, but I'll admit that there might be some possible drawbacks that I'm unaware of (other than it seeming like a bit of a hack).

这对我有用过好几次,但我承认可能存在一些我不知道的可能的缺点(除了它看起来有点像黑客)。