动态库使用静态库,出现未定义的符号
我一直在寻找解决问题的方法,只是得到了一些线索,但我找不到任何一致的解决方案: 我有一个动态库(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用链接器选项
--whole-archive
,它将包含整个静态库,并且可能会解决您的问题,尽管该库可能会变得相当大。人ld:
不要忘记关闭
-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.man ld:
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.