ld: i386 架构于输入文件 bar.o 与 i386:x86-64 输出不兼容

本文介绍了如何解决使用nasm编译的32位目标代码与gcc编译的64位目标代码在链接过程中的不兼容问题。提供了一种通过调整gcc编译选项以生成32位代码并进行32位链接的方法。

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

报错:ld: i386 架构于输入文件 foo.o 与 i386:x86-64 输出不兼容

或者:ld: i386 architecture of input file `foo.o' is incompatible with i386:x86-64 output

编译链接指令
1

1
nasm -f elf foo.s -o foo.o
2

1
gcc -c bar.c -o bar.o
3

1
ld -s -o foobar bar.o foo.o
汇编语言用nasm编写并用nasm编译器编译,而C语言用的是gcc编译,这些都没有问题,但是在链接的时候出错了,提示如下:
ld: i386 architecture of input file `foo.o' is incompatible with i386:x86-64 output

意思就是nasm 编译产生的是32位的目标代码,gcc 在64位平台上默认产生的是64位的目标代码,这两者在链接的时候出错,gcc在64位平台上默认以64位的方式链接。
这样在解决的时候就会有两种解决方案:
<1> 让gcc 产生32位的代码,并在链接的时候以32位的方式进行链接
在这种情况下只需要修改编译和链接指令即可,具体如下:
32位的编译链接指令
1

1
nasm -f elf foo.s -o foo.o
2

1
gcc -m32 -c bar.c -o bar.o
3

1
ld -m elf_i386 -s -o foobar foo.o bar.o
具体的-m32 和 -m elf_i386 请自行查阅gcc (man gcc)

转载于:https://www.cnblogs.com/liubiyonge/p/9405623.html

root@ubuntu:/zlab_16t2/changqingteng# echo "/usr/local/lib" | tee /etc/ld.so.conf.d/freesasa.conf /usr/local/lib root@ubuntu:/zlab_16t2/changqingteng# ldconfig -v | grep freesasa /sbin/ldconfig.real: Can't stat /usr/local/lib/i386-linux-gnu: No such file or directory /sbin/ldconfig.real: Path `/usr/lib/i386-linux-gnu' given more than once (from /etc/ld.so.conf.d/i386-linux-gnu.conf:4 and /etc/ld.so.conf.d/i386-linux-gnu.conf:3) /sbin/ldconfig.real: Can't stat /usr/local/lib/i686-linux-gnu: No such file or directory /sbin/ldconfig.real: Can't stat /lib/i686-linux-gnu: No such file or directory /sbin/ldconfig.real: Can't stat /usr/lib/i686-linux-gnu: No such file or directory /sbin/ldconfig.real: Path `/usr/local/lib' given more than once (from /etc/ld.so.conf.d/libc.conf:2 and /etc/ld.so.conf.d/freesasa.conf:1) /sbin/ldconfig.real: Can't stat /usr/local/lib/x86_64-linux-gnu: No such file or directory /sbin/ldconfig.real: Path `/usr/lib/x86_64-linux-gnu' given more than once (from /etc/ld.so.conf.d/x86_64-linux-gnu.conf:4 and /etc/ld.so.conf.d/x86_64-linux-gnu.conf:3) /sbin/ldconfig.real: Path `/usr/lib32' given more than once (from /etc/ld.so.conf.d/zz_i386-biarch-compat.conf:3 and /etc/ld.so.conf.d/zz_i386-biarch-compat.conf:2) /sbin/ldconfig.real: Path `/lib/x86_64-linux-gnu' given more than once (from <builtin>:0 and /etc/ld.so.conf.d/x86_64-linux-gnu.conf:3) /sbin/ldconfig.real: Path `/usr/lib/x86_64-linux-gnu' given more than once (from <builtin>:0 and /etc/ld.so.conf.d/x86_64-linux-gnu.conf:3) /sbin/ldconfig.real: Path `/usr/lib' given more than once (from <builtin>:0 and <builtin>:0) /sbin/ldconfig.real: /lib/i386-linux-gnu/ld-linux.so.2 is the dynamic linker, ignoring /usr/local/lib: (from /etc/ld.so.conf.d/freesasa.conf:1) /sbin/ldconfig.real: /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 is the dynamic linker, ignoring /sbin/ldconfig.real: /lib32/ld-linux.so.2 is the dynamic linker, ignoring 请分析错误原因,并给出详细解决步骤
最新发布
07-27
<think>我们正在解决配置动态链接库路径时出现的警告(如重复路径、无法访问的目录)以及确保libfreesasa.so被正确加载的问题。 根据引用[1],我们知道Linux动态库的默认搜索路径是/lib和/usr/lib,同时可以通过三种方法指定额外的路径:/etc/ld.so.conf配置文件、环境变量LD_LIBRARY_PATH和编译时指定rpath。 用户之前运行`find / -name libfreesasa.so 2>/dev/null`未找到该库,现在假设用户已经找到该库(例如在非标准路径如/opt/freesasa/lib下),但在配置路径时遇到了警告(如重复路径或无法访问的目录)。 解决步骤: 1. **确认库文件位置**: 首先,用户必须知道libfreesasa.so的确切位置。如果之前未找到,可能需要重新安装或查找。假设现在用户已经知道路径,例如:`/usr/local/freesasa/lib/libfreesasa.so` 2. **检查现有配置**: 查看/etc/ld.so.conf和/etc/ld.so.conf.d/下的配置文件,检查是否有重复的路径配置或指向不存在目录的配置。 使用以下命令查看当前加载的库路径: ```bash ldconfig -v 2>&1 | grep -v "^\s" ``` 或者查看缓存: ```bash ldconfig -p ``` 3. **处理重复路径**: 重复路径通常无害,但为了整洁,可以检查并删除重复项。检查以下文件- /etc/ld.so.conf - /etc/ld.so.conf.d/*.conf 使用以下命令可以检查重复行(以/etc/ld.so.conf.d/目录下的文件为例): ```bash # 检查所有.conf文件是否有重复行 grep -v '^#' /etc/ld.so.conf /etc/ld.so.conf.d/*.conf | sort | uniq -d ``` 如果发现重复路径,可以编辑相应的配置文件,删除重复行。 4. **处理无法访问的目录**: 在运行`sudo ldconfig`时,如果某个目录不存在或无法访问,会显示警告。例如: ``` ldconfig: cannot open directory /non/exist/dir: No such file or directory ``` 需要检查配置文件,将不存在的目录路径删除。 5. **添加新的库路径**: 在/etc/ld.so.conf.d/目录下创建一个新的配置文件(例如freesasa.conf),写入库所在的目录(注意:是包含库文件的目录,而不是库文件本身)。例如,如果库在`/usr/local/freesasa/lib`,则: ```bash echo "/usr/local/freesasa/lib" | sudo tee /etc/ld.so.conf.d/freesasa.conf ``` 6. **更新缓存并验证**: ```bash sudo ldconfig ``` 然后检查库是否被正确加载: ```bash ldconfig -p | grep libfreesasa ``` 如果输出中出现了libfreesasa.so,说明已经成功添加到缓存。 7. **处理环境变量冲突(可选)**: 如果使用了LD_LIBRARY_PATH,并且其中包含重复路径或不存在的路径,可能会引起问题。检查当前环境变量: ```bash echo $LD_LIBRARY_PATH ``` 如果发现有问题,可以编辑shell配置文件(如~/.bashrc)进行修改。 8. **测试加载**: 使用一个测试程序或直接使用ldd命令检查依赖该库的程序: ```bash ldd /path/to/your_program | grep libfreesasa ``` 或者使用动态加载器直接测试: ```bash /lib64/ld-linux-x86-64.so.2 --list /path/to/your_program ``` 9. **处理警告**: 如果执行`sudo ldconfig`后仍然有警告,根据警告信息(如重复路径或无法访问的目录)定位到具体的配置文件,然后进行清理。 10. **确保库文件权限正确**: 确保库文件可被读取: ```bash sudo chmod 755 /usr/local/freesasa/lib/libfreesasa.so ``` 总结:通过清理重复路径、删除无效路径、添加正确的库路径并更新缓存,可以解决警告并确保库被正确加载。 注意:在修改系统配置文件时,请谨慎操作,建议备份。 下面我们将这些步骤整理成清晰的回答。</think>### 解决动态链接库路径配置中的警告加载问题 当配置动态链接库路径时出现"重复路径"或"无法访问目录"警告,通常是由于路径配置冲突或无效路径导致。以下是系统化的解决方案: #### 1. **诊断路径配置问题** ```bash # 检查当前动态库搜索路径 echo -e "\n当前系统库路径:" ldconfig -v 2>&1 | grep -v "^/\|^[[:space:]]" # 检查重复路径 sudo grep -rh "/path/to/libs" /etc/ld.so.conf.d/ | sort | uniq -d # 验证目录可访问性 ls -ld /path/to/libs 2>/dev/null || echo "目录不存在" ``` - 重复路径警告:表示同一路径被多次注册(如多个.conf文件包含相同路径) - 无法访问目录:路径不存在或权限不足(错误代码`ENOENT`) #### 2. **清理无效配置(关键步骤)** ```bash # 备份配置 sudo tar -czf /root/ld_backup_$(date +%F).tar.gz /etc/ld.so.conf.d/ # 删除重复/无效配置 sudo find /etc/ld.so.conf.d/ -type f -exec grep -l "/invalid/path" {} + | xargs sudo rm -f # 删除空文件 sudo find /etc/ld.so.conf.d/ -empty -delete ``` #### 3. **正确配置libfreesasa.so路径** ```bash # 创建专属配置文件 echo "/usr/local/freesasa/lib" | sudo tee /etc/ld.so.conf.d/99-freesasa.conf # 验证路径有效性(必须返回libfreesasa.so) ls /usr/local/freesasa/lib/libfreesasa.so || echo "错误:库文件不存在" # 更新系统库缓存 sudo ldconfig -v 2>&1 | grep -i "freesasa" ``` ✅ 预期输出:`libfreesasa.so -> libfreesasa.so.2.0.3`(版本号可能不同) #### 4. **权限修复(如遇访问拒绝)** ```bash # 修复库目录权限 sudo chmod 755 /usr/local/freesasa/lib sudo chmod 755 /usr/local/freesasa/lib/libfreesasa.so* # 修复SELinux上下文(如启用) sudo restorecon -Rv /usr/local/freesasa/lib ``` #### 5. **验证库加载功能** ```bash # 测试库加载能力 cat <<EOF | gcc -xc - -lfreesasa -o /tmp/libsasa_test && /tmp/libsasa_test #include <freesasa.h> int main() { printf("FreeSASA v%s loaded\n", freesasa_string_version()); return 0; } EOF ``` ✅ 预期输出:`FreeSASA v2.1.0 loaded`(版本号可能不同) #### 故障排除矩阵 | 警告类型 | 原因 | 解决方案 | |---------|------|----------| | `重复路径` | 多个.conf文件包含相同路径 | 删除重复配置,保留单个引用 | | `无法访问目录` | 路径不存在/权限不足 | `chmod 755`或修正路径 | | `libfreesasa.so未找到` | 路径未注册/缓存未更新 | 执行步骤3创建专属配置 | | `权限拒绝` | SELinux限制/文件权限 | 执行步骤4权限修复 | > **原理说明**:Linux通过`ldconfig`管理动态库缓存,重复路径会增加加载开销,无效路径会导致运行时加载失败。配置优先级:`LD_LIBRARY_PATH` > `/etc/ld.so.conf.d/` > 默认路径`(/lib,/usr/lib)`[^1]。 #### 永久生效配置示例 ```conf # /etc/ld.so.conf.d/99-freesasa.conf 内容示例 # 单行指定有效路径(避免注释引起解析问题) /usr/local/freesasa/lib ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值