加入收藏 | 设为首页 | 会员中心 | 我要投稿 辽源站长网 (https://www.0437zz.com/)- 云专线、云连接、智能数据、边缘计算、数据安全!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

linux – 来自便利库的符号未在可执行文件中导出

发布时间:2021-01-15 16:10:39 所属栏目:Linux 来源:网络整理
导读:我有一个程序,myprogram,它与一个静态便利库链接,称之为libconvenience.a,它包含一个函数func().在myprogram中的任何地方都不调用函数func();它需要能够从插件库plugin.so中调用. 符号func()未在myprogram中动态导出.如果我跑 nm myprogram | grep func 我一

我有一个程序,myprogram,它与一个静态便利库链接,称之为libconvenience.a,它包含一个函数func().在myprogram中的任何地方都不调用函数func();它需要能够从插件库plugin.so中调用.

符号func()未在myprogram中动态导出.如果我跑

nm myprogram | grep func

我一无所获.但是,libconvenience.a并没有遗漏它:

nm libconvenience/libconvenience.a | grep func
00000000 T func

我正在使用automake,但是如果我在命令行上手动执行最后一个链接步骤,它也不起作用:

gcc -Wl,--export-dynamic -o myprogram *.o libconvenience/libconvenience.a `pkg-config --libs somelibraries`

但是,如果我像这样链接程序,跳过使用便利库并链接直接进入libconvenience.a的目标文件,func()会显示在myprogram的符号中:

gcc -Wl,--export-dynamic -o myprogram *.o libconvenience/*.o `pkg-config --libs somelibraries`

如果我在myprogram中的某处向func()添加一个虚拟调用,那么func()也会显示在myprogram的符号中.但我认为–export-dynamic应该导出所有符号,无论它们是否在程序中使用过!

我在Fedora 14上使用automake 1.11.1和gcc 4.5.1.我也使用Libtool 2.2.10来构建plugin.so(但不是便利库.)

我没有忘记在myprogram_LDFLAGS中放置-Wl,– export-dynamic,也没有忘记在libconvenience_a_SOURCES中放置包含func()的源代码(一些Google搜索表明这些是导致此问题的常见原因.)

有人可以帮我理解这里发生了什么吗?

最佳答案 我设法解决了它.正是约翰卡尔科特出色的Autotools书中的这一说明指出了我正确的方向:

Linkers add to the binary product every object file specified explicitly on the command line,but they only extract from archives those object files that are actually referenced in the code being linked.

要抵消这种行为,可以将–whole-archive标志用于libtool.但是,这会导致所有系统库中的所有符号也被拉入,从而导致大量双符号定义错误.所以–whole-archive需要在链接器命令行上的libconvenience.a之前正确,并且需要后跟–no-whole-archive,以便其他库不会被这样处理.这有点困难,因为automake和libtool并不能保证在命令行中保持标志的顺序相同,但Makefile.am中的这一行做了诀窍:

myprogram_LDFLAGS = -Wl,--export-dynamic 
    -Wl,--whole-archive,libconvenience/libconvenience.a,--no-whole-archive

(编辑:辽源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读