防止从静态库导入函数
假设我有两个不是由我构建的静态库,并且我无法控制它们的内容。
库 1 有函数:
A()
B()
C()
库 2 有函数:
A()
D()
E()
两者都需要链接到调用应用程序,但 A()
的命名冲突会引发错误。
有没有办法在 Linux 中使用 gcc 和 ld 说“链接时忽略库 1 中的 A()”。
Say I have two static libraries that were not built by me and I have no control over their contents.
Library 1 has functions:
A()
B()
C()
Library 2 has functions:
A()
D()
E()
Both need to be linked into a calling application but the naming conflict of A()
throws errors.
Is there a way to say "Ignore A() from Library 1 when linking" in linux using gcc and ld.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道有几种方法:
您可以制作隐藏了相关符号的库的副本,并链接到该副本。您不需要访问库代码的任何源代码即可执行此操作:
objcopy
可以使用--localize-symbol
选项来完成此操作。我在类似问题的此答案中描述了如何使用.o
文件执行此操作,但是同样的方法对于.a
库也适用。--allow-multiple可以使用-definition
选项。 (如果您通过gcc
命令进行链接,而不是直接使用ld
进行链接,则需要将选项指定为-Wl,--allow-多重定义。)这将导致链接器停止关心多重定义,而只是使用它遇到的第一个定义 - 因此您必须小心库在命令行上出现的顺序。缺点是它是一个全局选项,因此如果您有其他意外的符号冲突,它可能会做错误的事情而不是告诉您。
There are a couple of methods that I know of:
You could make a copy of the library which has the relevant symbol hidden, and link against the copy. You don't need access to any of the source for the library code to do this:
objcopy
can do it with the--localize-symbol
option. I describe how to do this with.o
files in this answer to a similar question, but the same method works just as well with.a
libraries.The
--allow-multiple-definition
option could be used. (If you're linking via agcc
command, rather than withld
directly, you'll need to specify the option as-Wl,--allow-multiple-definition
.) This will cause the linker to stop caring about the multiple definition, and simply use the first one that it encounters instead - so you have to be careful what order the libraries appear in on the command line. The downside it that it's a global option, so if you have other unexpected symbol clashes, it might quitely do the wrong thing instead of telling you about it.