链接共享库的依赖项 [英] Linking dependencies of a shared library

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

问题描述

我正在使用SFML,我编译了一个小测试程序并添加了链接选项 -lsfml-audio .然后,我使用 ldd ./program 来查看链接到的动态库.令人惊讶的是,有很多东西,我都没有在我的makefile文件中手动选择它们,也没有使用 pkg-config --libs 来选择.

I was working with SFML, I compiled a little test program and added the linkage option -lsfml-audio. Then, I used ldd ./program to see the dynamic libraries it was linking to. Surprisingly, there were a lot, none of them had I manually selected in my makefile, nor using pkg-config --libs.

我开始阅读有关共享库的信息,并举了一个小例子来解决我的疑问.但是,我有一个问题:

I started reading about shared libraries, and made a little example to solve my doubts. However, I have this question:

为什么某些库需要您在生成文件中添加依赖项(手动或使用诸如 pkg-config 之类的脚本)和其他库会自动链接它们的依赖项?

why some libraries need you to add the dependencies in your makefile (either manually or using a script like pkg-config) and other libraries automatically link their dependencies?

创建动态库时,就像在 g ++ -shared ... 命令中添加适当的 -ldependency 选项一样简单,以避免用户稍后手动添加依赖项的麻烦.为什么许多可用的库都不这样做?

When you're creating your dynamic library, is just as easy as adding the proper -ldependency options in the g++ -shared ... command to avoid the user the hassle of manually adding the dependencies later on. Why many of the available libraries don't do that?

我猜想这必须与微调链接到哪些库等的能力有关.

I guess it must be related to the ability of fine tuning which libraries get linked and such.

推荐答案

共享库通常会链接其依赖项.但是,静态库无法执行此操作. pkg-config --libs 通常包括所有依赖项(直接和间接),因此您可以通过简单地添加 -static 来切换到静态编译,而不必添加其他库依赖项,例如好吧.

Shared libraries will generally link in their dependencies. However, static libraries are not capable of doing so. pkg-config --libs often includes all dependencies (direct and indirect) so that you can switch to static compilation by simply adding -static without having to add additional library dependencies as well.

请注意,在某些情况下,这些多余的直接依赖项被认为是不需要的(例如,debian试图避免在打包的二进制文件中使用它们,因为它们使库soname过渡变得比必需的更具创伤性).您可以使用 -Wl ---按需标志,指示链接器从最终可执行文件中剥离直接依赖项.

Note that these excess direct dependencies are considered unwanted in some cases (eg, debian tries to avoid them in packaged binaries, as they make library soname transitions more traumatic than necessary). You can instruct the linker to strip direct dependencies from the final executable that aren't needed with the -Wl,--as-needed flag.

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

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