便利库中的符号未导出到可执行文件中
我有一个程序 myprogram
,它与一个静态便利库链接,将其称为 libconvenience.a
,其中包含一个函数 func()
>。 func()
函数在 myprogram
中没有被调用;它需要能够从插件库 plugin.so
中调用。
符号 func()
未在 myprogram
中动态导出。如果我跑步,
nm myprogram | grep func
我什么也得不到。然而,libconvenience.a
中并没有缺少它:
nm libconvenience/libconvenience.a | grep 函数
00000000T函数
我正在使用 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
中' s 符号。但我认为 --export-dynamic
应该导出所有符号,无论它们是否在程序中使用!
我在 Fedora 14 上使用 automake 1.11.1 和 gcc 4.5.1。我还使用 Libtool 2.2.10 构建 plugin.so
(但不是便利库。)
我没有忘记将 -Wl,--export-dynamic
放入 myprogram_LDFLAGS
中,我也没有忘记将包含 func()
的源代码放入 中libconvenience_a_SOURCES
(一些谷歌搜索表明这些是此问题的常见原因。)
有人可以帮助我理解这里发生了什么吗?
I have a program, myprogram
, which is linked with a static convenience library, call it libconvenience.a
, which contains a function, func()
. The function func()
isn't called anywhere in myprogram
; it needs to be able to be called from a plugin library, plugin.so
.
The symbol func()
is not getting exported dynamically in myprogram
. If I run
nm myprogram | grep func
I get nothing. However, it isn't missing from libconvenience.a
:
nm libconvenience/libconvenience.a | grep func
00000000 T func
I am using automake, but if I do the last linking step by hand on the command line instead, it doesn't work either:
gcc -Wl,--export-dynamic -o myprogram *.o libconvenience/libconvenience.a `pkg-config --libs somelibraries`
However, if I link the program like this, skipping the use of a convenience library and linking the object files that would have gone into libconvenience.a
directly, func()
shows up in myprogram
's symbols as it should:
gcc -Wl,--export-dynamic -o myprogram *.o libconvenience/*.o `pkg-config --libs somelibraries`
If I add a dummy call to func()
somewhere in myprogram
, then func()
also shows up in myprogram
's symbols. But I thought that --export-dynamic
was supposed to export all symbols regardless of whether they were used in the program or not!
I am using automake 1.11.1 and gcc 4.5.1 on Fedora 14. I am also using Libtool 2.2.10 to build plugin.so
(but not the convenience library.)
I didn't forget to put -Wl,--export-dynamic
in myprogram_LDFLAGS
, nor did I forget to put the source that contains func()
in libconvenience_a_SOURCES
(some Googling suggests that these are common causes of this problem.)
Can somebody help me understand what is going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我设法解决了它。约翰·卡尔科特 (John Calcote) 的优秀 Autotools 书中的这条注释为我指明了正确的方向:
为了抵消这种行为,可以对 libtool 使用
--whole-archive
标志。但是,这会导致所有系统库中的所有符号也被拉入,从而导致大量双符号定义错误。因此,在链接器命令行上,--whole-archive
需要位于libconvenience.a
之前,并且后面需要紧跟--no-whole- archive
以便其他库不会被这样对待。这有点困难,因为 automake 和 libtool 并不能真正保证您的标志在命令行上保持相同的顺序,但是Makefile.am
中的这一行做到了这一点:I managed to solve it. It was this note from John Calcote's excellent Autotools book that pointed me in the right direction:
To counteract this behavior, one can use the
--whole-archive
flag to libtool. However, this causes all the symbols from all the system libraries to be pulled in also, causing lots of double symbol definition errors. So--whole-archive
needs to be right beforelibconvenience.a
on the linker command line, and it needs to be followed by--no-whole-archive
so that the other libraries aren't treated that way. This is a bit difficult since automake and libtool don't really guarantee keeping your flags in the same order on the command line, but this line inMakefile.am
did the trick:如果您需要 func 位于 plugin.so 中,则应尽可能尝试在那里找到它。便利库就是为了方便地链接到可执行文件或库作为中间步骤。
If you need func to be in plugin.so, you should try and locate it there if possible. Convenience libraries are meant to be just that -- a convenience to link to an executable or lib as an intermediate step.