便利库中的符号未导出到可执行文件中

发布于 2024-10-01 19:37:33 字数 1455 浏览 10 评论 0原文

我有一个程序 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪菊怪哟 2024-10-08 19:37:33

我设法解决了它。约翰·卡尔科特 (John Calcote) 的优秀 Autotools 书中的这条注释为我指明了正确的方向:

链接器将在命令行上明确指定的每个目标文件添加到二进制产品中,但它们仅从存档中提取正在链接的代码中实际引用的那些目标文件。

为了抵消这种行为,可以对 libtool 使用 --whole-archive 标志。但是,这会导致所有系统库中的所有符号也被拉入,从而导致大量双符号定义错误。因此,在链接器命令行上,--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

I managed to solve it. It was this note from John Calcote's excellent Autotools book that pointed me in the right direction:

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.

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 before libconvenience.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 in Makefile.am did the trick:

myprogram_LDFLAGS = -Wl,--export-dynamic \
    -Wl,--whole-archive,libconvenience/libconvenience.a,--no-whole-archive
心欲静而疯不止 2024-10-08 19:37:33

如果您需要 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文