链接器如何解析动态可加载库中的重复符号? [英] How does linker resolves duplicate symbols in dynamically loadable libraries?

查看:25
本文介绍了链接器如何解析动态可加载库中的重复符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个可动态加载的库 lib_smtp.so 和 libpop.so 等.两者都有一个名为 protocol 的全局变量,分别初始化为SMTP"和POP".我有另一个静态库 libhttp.a,其中 protocol 被初始化为HTTP".

I have two dynamically loadable libraries lib_smtp.so and and libpop.so etc. Both have a global variable named protocol which is initialized to "SMTP" and "POP" respectively. I have another static library libhttp.a where protocol is initialized to "HTTP".

现在由于某种原因,我需要静态编译所有动态可链接和可加载库并包含在可执行文件中.这样做我在链接静态库期间收到错误multiple definition of symbol".

Now for some reason i need to compile all dynamic linkable and loadable libraries statically and include in the executable. Doing so i am getting error "multiple definition of symbol" during linking of static libraries.

我很想知道链接器在动态链接期间如何解决重复的符号,其中所有三个提到的库都被链接?

I am curious to know how linker resolves duplicate symbols during dynamic linking where all three mentioned libraries are getting linked ?

有没有什么方法我可以静态地做与链接器在动态链接中所做的相同的事情,即在没有任何冲突的情况下将所有静态库添加到具有相同符号的可执行文件中?如果不是,为什么静态链接库的过程不同.

Is there some way i can do the same statically as linker is doing in dynamic linking ie without any conflict add all static libraries to executable which have same symbols? if not, why the process is different for statically linked libraries.

推荐答案

现代 Linux 和其他几个操作系统中的动态链接基于 ELF 二进制格式.可执行文件或其他共享库所依赖的 (ELF) 动态库具有优先级.为了解析给定的符号,动态链接器按优先级顺序检查每个库,直到找到定义该符号的库.

Dynamic linking in modern Linux and several other operating systems is based on the ELF binary format. The (ELF) dynamic libraries on which an executable or other shared library relies are prioritized. To resolve a given symbol, the dynamic linker checks each library in priority order until it finds one that defines the symbol.

当多个动态对象定义相同的符号并且多个动态对象使用该符号时,这可能会很冒险.然后可能是符号在不同的动态对象中以不同的方式解析.

That can be dicey when multiple dynamic objects define the same symbol and also multiple dynamic objects use that symbol. It can then be the case that the symbol is resolved differently in different dynamic objects.

完整的细节超出了 SO 的范围,但我不知道比 Ulrich Drepper 的论文如何编写共享库".

Full details are out of scope for SO, but I don't know a better technical explanation than the one in Ulrich Drepper's paper "How to Write Shared Libraries".

这篇关于链接器如何解析动态可加载库中的重复符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆