问题现象
在按照Linux From Scratch(LFS)手册编译GCC 4.5.2时,执行make
命令后出现以下错误:
error while loading shared libraries: libgmp.so.10: cannot open shared object file: No such file or directory
环境背景
编译环境为Ubuntu 11系统,配置参数如下:
./configure \
--target=$LFS_TGT --prefix=/tools \
--disable-nls --disable-shared --disable-multilib \
--disable-decimal-float --disable-threads \
--disable-libmudflap --disable-libssp \
--disable-libgomp --enable-languages=c \
--with-gmp-include=$(pwd)/gmp --with-gmp-lib=$(pwd)/gmp/.libs \
--without-ppl --without-cloog
根本原因
这个问题通常发生在以下两种情况:
- 系统缺少GMP数学库的共享对象文件
- 虽然GMP已编译,但动态链接器无法找到库文件位置
解决方案
以下是几种可行的解决方法:
方法1:设置LD_LIBRARY_PATH
临时指定库文件搜索路径:
export LD_LIBRARY_PATH=$(pwd)/gmp/.libs:$LD_LIBRARY_PATH
make
方法2:创建符号链接
将编译好的GMP库链接到系统库目录:
sudo ln -s $(pwd)/gmp/.libs/libgmp.so.10 /usr/lib/
ldconfig
方法3:重新配置安装GMP
确保GMP正确安装到系统目录:
cd gmp
./configure --prefix=/usr/local
make
sudo make install
ldconfig
验证方法
执行以下命令验证库文件是否可被找到:
ldconfig -p | grep libgmp
# 或
LD_DEBUG=libs ldd /path/to/gcc/compiler
注意事项
- 在LFS构建过程中,建议使用方法1保持工具链隔离
- 如果使用交叉编译,需要确保目标架构匹配
- Ubuntu 11系统较旧,可能需要手动安装较新版本的GMP