动态库使用静态库,出现未定义的符号

发布于 2024-11-04 06:45:37 字数 498 浏览 5 评论 0原文

我一直在寻找解决问题的方法,只是得到了一些线索,但我找不到任何一致的解决方案: 我有一个动态库(libdyna.so)的代码,它使用3个静态库(libone.a、libtwo.a、lib Three.a)和log4cpp库的功能。 当我第一次构建它时,一切看起来都很好,我可以制作“libdyna.so”,但是当你通过包装器(jni 包装器工作正常)用我的 java 测试程序测试它时,我得到了很多未定义的引用。

然后我将“-z defs”标志放入make文件中,出现了很多未定义的引用,我无法再次编译动态库。

我已经使用三个静态库创建了“nm”命令,它正确导出了我在 dyna 库中使用的所有函数。但是当我对包含 3 个静态库的“libdyna.so”进行 nm 操作时,我发现了未定义的符号(函数)。

我认为解决方案必须类似于当我想制作动态链接器时向链接器添加一些标志,但我真的不知道。 有人可以帮助我,或者告诉我一些想法吗?如果需要一些代码,请告诉我,我会将其粘贴到此处。 多谢。

P/D:抱歉我的英语不好。

I've look for a solutions to my trouble and just get some clues, but I could not find any consistent solution:
I have the code of a dynamic library (libdyna.so), that uses the functions of 3 statics libraries (libone.a, libtwo.a, libthree.a) and the log4cpp library.
And when I built it the first time everything looked fine, I could make the 'libdyna.so', but when y tested it with my java test program though a wrapper (the jni wrapper works fine) I got lots of undefined references.

Then I put the "-z defs" flag to the make file and a lot of undefined references appear and I could not compile the dynamic library again.

I've made the 'nm' command with the three statics libraries and it exports all the functions that I use in the dyna lib correctly. But when I made nm over the 'libdyna.so', the one which includes the 3 statics libs, I found Undefined symbols (functions).

I think that the solutions must be something like adding some flag to the linker when I want to made the dynamic one, but i really don't know.
Could anyone help me, or tell me some ideas?. if some code is needed please let me know and I'll paste it here.
Thanks a lot.

P/D: Sorry for my bad english.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

苯莒 2024-11-11 06:45:37

您可以使用链接器选项--whole-archive,它将包含整个静态库,并且可能会解决您的问题,尽管该库可能会变得相当大。

g++ -shared -o libdyna.so dyna.o -Wl,-whole-archive -la -lb -lc -Wl,-no-whole-archive

人ld:

对于命令行上 --whole-archive 选项之后提到的每个存档,将存档中的每个目标文件包含在
链接,而不是在档案中搜索所需的目标文件。这通常用于将存档文件转换为共享文件
库,强制每个对象都包含在生成的共享库中。

不要忘记关闭-Wl,-no-whole-archive

还要检查库在 gcc 命令行中出现的顺序,确保它们位于动态库的目标文件之后,否则链接器将不会加载它们。例如,如果 libone 使用 libtwo,那么 libone 出现在 libtwo 之前也很重要。它可能是您问题的替代解决方案。

You could use linker option --whole-archive, it will include whole static libraries and will probably solve your problem, although the library could become quite big.

g++ -shared -o libdyna.so dyna.o -Wl,-whole-archive -la -lb -lc -Wl,-no-whole-archive

man ld:

For each archive mentioned on the command line after the --whole-archive option, include every object file in the archive in the
link, rather than searching the archive for the required object files. This is normally used to turn an archive file into a shared
library, forcing every object to be included in the resulting shared library.

Don't forget closing -Wl,-no-whole-archive.

Check also the order of appearing of the libraries in gcc command line, make sure they are after the object files of the dynamic library, otherwise the linker will not load them. If libone uses libtwo for example, it's also important for libone to appear before libtwo. It could be alternative solution for your problem.

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