共享库的CMake和顺序依赖链接 [英] CMake and order dependent linking of shared libraries

查看:2291
本文介绍了共享库的CMake和顺序依赖链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  add_library(liba SHARED a.cpp)

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

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

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

  target_link_libraries(dummy ba)



程序编译和链接很好



当这种系统开始涉及更复杂依赖的库,它开始是不可能的,即使依赖是非循环的。如何在这里管理链接步骤?

解决方案

您可以指定 a 和 b 添加

  target_link_libraries ba)



docs


默认情况下,库依赖项是传递性的。当此目标链接到另一个目标时,链接到该目标的库也将出现在其他目标的链接线上。


所以,如果你以这种方式指定 a 作为 b 的依赖,你甚至不需要显式列出 a b

  target_link_libraries(dummy b)

虽然它不会对列表 a 造成任何伤害。


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

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

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?

解决方案

You can specify the relationship between a and b by adding

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 a as a dependency of b in this way, you don't even need to explicitly list a in any target which depends on b, i.e. your other command can be just:

target_link_libraries(dummy b)

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

这篇关于共享库的CMake和顺序依赖链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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