简体   繁体   English

链接共享库的依赖项

[英]Linking dependencies of a shared library

I was working with SFML, I compiled a little test program and added the linkage option -lsfml-audio . 我正在使用SFML,我编译了一个小测试程序并添加了链接选项-lsfml-audio Then, I used ldd ./program to see the dynamic libraries it was linking to. 然后,我使用ldd ./program来查看它链接到的动态库。 Surprisingly, there were a lot, none of them had I manually selected in my makefile, nor using pkg-config --libs . 令人惊讶的是,有很多东西,我都没有手动在makefile中选择它们,也没有使用pkg-config --libs

I started reading about shared libraries, and made a little example to solve my doubts. 我开始阅读有关共享库的信息,并举了一个小例子来解决我的疑问。 However, I have this question: 但是,我有这个问题:

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? 为什么某些库需要您在makefile中添加依赖项(手动或使用类似pkg-config类的脚本),而其他库会自动链接其依赖项?

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. 创建动态库时,就像在g++ -shared ...命令中添加适当的-ldependency选项一样容易,以避免用户日后手动添加依赖项的麻烦。 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. 我想这一定与微调哪些库被链接等能力有关。

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. pkg-config --libs通常包含所有依赖项(直接和间接),因此您可以通过简单地添加-static来切换到静态编译,而不必也添加其他库依赖项。

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). 请注意,在某些情况下,这些多余的直接依赖关系被认为是不需要的(例如,debian试图避免在打包的二进制文件中使用它们,因为它们使库soname过渡变得比需要的更痛苦)。 You can instruct the linker to strip direct dependencies from the final executable that aren't needed with the -Wl,--as-needed flag. 您可以指示链接器使用-Wl,--as-needed标志从最终可执行文件中剥离不需要的直接依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM