C ++为什么这种特定情况导致符号链接错误? [英] C++ Why does this specific situation cause a symbol linking error?

查看:101
本文介绍了C ++为什么这种特定情况导致符号链接错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,其中有一个核心和多个插件。我在Linux上使用Qt,我使用Qt的插件加载器系统。插件创建为共享对象文件(* .so)并动态加载。如果我链接到带有插件的库,并且该库链接到其他库,我经常从应用程序得到一个'未定义的符号'错误。要解决这个问题,我需要将插件链接到其他库。



插件链接到LibA



LibA链接到LibB,LibC



LibA编译为静态。当我尝试加载插件,我会得到一个未定义的符号错误,像这样:

 无法加载共享库' myPluginName.so':
myPluginName.so:undefined symbol:_ZN3BlahBlahD2Ev

可以使用filt解开符号名称,找出符号所属的lib(例如LibB),然后编译为:



插件链接到LibA,LibB

LibA链接到LibB,LibC



我不知道为什么会发生错误。如果LibA链接到LibB和LibC,为什么Plugin必须知道关于LibB?为什么在所有情况下都不会出现此错误(即没有与使用LibC的未定义符号相关的错误)?



我会感谢任何输入。



-kf

解决方案

您必须记住,静态库一个对象文件的集合打包在一起成一个单一的文件。如果你的插件需要LibA,而LibA需要LibB和LibC,那么当你链接你的插件时,你必须告诉链接器LibA,LibB和LibC。



使用动态库,这将是一个不同的故事。 .so已经链接,所以它的所有引用已经解决。如果LibA是动态链接库,那么你的插件只需要链接到LibA.so,因为已经发生了链接步骤来将LibA绑定到它自己的依赖项。


I'm working on an application where there's a 'core' and multiple plugins. I'm using Qt on Linux, and I use Qt's plugin loader system. Plugins are created as shared object files (*.so) and loaded dynamically. If I link to a library with a plugin, and that library links to additional libraries, I often get an 'undefined symbol' error from the application. To get around it, I need to link the plugin to the additional libraries as well...

Plugin links to LibA

LibA links to LibB, LibC

LibA is compiled as static. When I try to load the plugin, I'll get an undefined symbol error, like so:

unable to load shared library 'myPluginName.so':
myPluginName.so: undefined symbol: _ZN3BlahBlahD2Ev

To fix this I can use filt to unmangle the symbol name, figure out which lib the symbol belongs in (say LibB), and then compile like:

Plugin links to LibA, LibB

LibA links to LibB, LibC

I don't know why the error occurs. If LibA links to LibB and LibC, why should Plugin have to 'know' about LibB as well? Why doesn't this error occur in all cases (ie no errors related to undefined symbols with LibC)?

I'd appreciate any input.

-kf

解决方案

You have to keep in mind that a static library is nothing more than a collection of object files packaged together into a single file. If your plugin needs LibA, and LibA needs LibB and LibC, then when you link your plugin you have to tell the linker where LibA, LibB and LibC are.

If you work with dynamic libraries it would be a different story. A .so is already linked, so all its references were already resolved. If LibA was a dynamically linked library then your plugin would only need to link against LibA.so, since a link step already occurred to bind LibA to its own dependencies.

这篇关于C ++为什么这种特定情况导致符号链接错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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