动态库编译时(引用静态库)could not read symbols: Bad value

在编译动态库时遇到`relocation R_X86_64_32 against 'a local symbol'`错误,提示需要使用`-fPIC`参数。问题可能源于编译.o文件或静态库时未添加此参数。解决方案是确保所有编译过程都包含`-fPIC`,以创建符合要求的动态库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


编译动态库时遇到relocation R_X86_64_32 against `a local symbol'的错误  

编译动态库时遇到如下错误:

... relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC

... could not read symbols: Bad value

解决办法编译器已经提示了:recompile with -fPIC

但是我们应该重新编译谁带上这个参数呢?经过我几番折腾,发现如下情况:

1、编译.o文件的时候,没有加参数-fPIC,这问题个在gcc version 3.4.6版本没有发生,可能那个编译器默认都给加上吧。

2、当前程序中有用到某个静态库,那个静态库编译.o文件的时候没有加上-fPIC(静态库其实就是.o文件打包)。补充一点:我发现手写Makefile时即使不加-fPIC也没有这个问题,这一点很郁闷,这个只在用automake工具编译出.a文件时才出现过。

知道原因了,解决办法自然有了,保证你编译.o文件的时候,都加上-fPIC,这样你才能编译出动态库来。

**************

I'm Trying to Link a static Library to a shared library , I'm Getting the Following error

/usr/bin/ld: ../../../libraries/log4cplus/liblog4cplus.a(fileappender.o): relocation R_X86_64_32S against `a local symbol' can not be used when making a shared object; recompile with -fPIC
../../../libraries/log4cplus/liblog4cplus.a: could not read symbols: Bad value
collect2: ld returned 1 exit status

Assuming you are generating a shared library, most probably what happens is that the variant of liblog4cplus.a you are using wasn't compiled with -fPIC. In linux, you can confirm this by extracting the object files from the static library and checking their relocations:

ar -x liblog4cplus.a  
readelf --relocs fileappender.o | egrep '(GOT|PLT|JU?MP_SLOT)'

If the output is empty, then the static library is not PIC and cannot be used to generate a shared object.

Since the static library contains object code which was already compiled, providing the -fPIC flag won't help.

You need to get ahold of a version of liblog4cplus.a compiled with -fPIC and use that one instead.


在C编译过程中,遇到 `ld returned 1 exit status` 错误通常表示链接器(`ld`)在尝试生成可执行文件或共享库时遇到了问题。该错误本身只是一个总括性指示,具体的失败原因需要通过查看详细的编译日志来进一步分析。以下是常见的导致该错误的原因及相应的解决方法。 ### 1. **未定义的引用(Undefined References)** - **现象**:链接器报告找不到某些函数或变量的定义。 - **解决方法**:确保所有使用的函数和全局变量在某个目标文件或库中都有定义。如果使用了外部库,需在编译命令中正确链接这些库(例如 `-lm` 用于数学库)。 ### 2. **重复定义的符号(Multiple Definitions)** - **现象**:同一个符号在多个目标文件中被定义。 - **解决方法**:检查代码中是否存在重复定义的全局变量或函数,尤其是头文件中可能被多次包含的定义。可以使用 `#ifndef` 宏保护来防止重复包含。 ### 3. **架构不匹配(Architecture Mismatch)** - **现象**:目标文件或库的架构与当前编译环境不匹配(例如32位与64位混合使用)。 - **解决方法**:确保所有目标文件和库的架构一致。可以使用 `file` 命令检查目标文件的架构,例如 `file myobject.o`。 ### 4. **缺少位置无关代码(Position Independent Code, PIC)** - **现象**:编译共享库时出现类似 `relocation R_X86_64_32 against .rodata` 的错误[^1]。 - **解决方法**:在编译目标文件时添加 `-fPIC` 选项,以生成位置无关代码。例如: ```bash gcc -fPIC -c mysource.c -o mysource.o ``` ### 5. **链接器无法找到符号表** - **现象**:链接器报告 `could not read symbols` 或 `Bad value` 等错误[^1]。 - **解决方法**:确保所有目标文件都正确生成且未损坏。重新编译相关文件,并检查编译命令是否正确。 ### 6. **编译工具链配置问题** - **现象**:在交叉编译或使用特定构建系统(如 Yocto)时出现编译失败[^2]。 - **解决方法**:检查工具链配置是否正确,包括编译器路径、目标架构设置以及依赖库的可用性。查看详细的构建日志以定位具体问题。 ### 7. **内核模块编译问题** - **现象**:在编译Linux内核模块时出现链接错误[^3]。 - **解决方法**:确保内核源码树结构完整,且 `Makefile` 配置正确。检查是否缺少必要的头文件或依赖模块。 ### 示例修复命令 以下是一个修复共享库链接错误的典型命令: ```bash gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -fPIC myobject.o -o mylibrary.so ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值