ld.so.conf 和 ldconfig

在遇到编译后的程序运行时出现错误提示,提示无法加载共享库libiconv.so.2时,通过在/etc/ld.so.conf中添加库路径并运行ldconfig解决了问题。解释了ldconfig命令的使用方法,包括其参数选项和作用,以及如何通过ldconfig命令管理和维护系统动态链接库。

本文来自:

http://blog.youkuaiyun.com/joliny/article/details/1679756


今天重新编译以前的一个程序,里面用到iconv库:gcc test.cc -liconv
运行时:a.out:error while loading shared libraries: libiconv.so.2: cannot open shared object file: No such file or directory
以前编译运行是可以的,可能是不久前升级了iconv库影响。在/usr/local/lib下可以找到libiconv.so.2,把/usr/local/lib加到路径中也不行。
google了一下,解决了:在/etc/ld.so.conf中加一行/usr/local/lib,运行ldconfig。再运行a.out,行了。
ld.so.conf和ldconfig是维护系统动态链接库的。真不明白为什么iconv库安装时不把这一步也做了。

 

 ldconfig命令行用法如下:    
     
  ldconfig   [-v|--verbose]   [-n]   [-N]   [-X]   [-f   CONF]   [-C   CACHE]   [-r   ROOT]   [-l]   [-p|--print-cache]   [-c   FORMAT]   [--format=FORMAT]   [-V]   [-?|--help|--usage]   path...    
     
  ldconfig可用的选项说明如下:    
     
  (1)   -v或--verbose   :   用此选项时,ldconfig将显示正在扫描的目录及搜索到的动态链接库,还有它所创建的连接的名字.    
     
  (2)   -n   :   用此选项时,ldconfig仅扫描命令行指定的目录,不扫描默认目录(/lib,/usr/lib),也不扫描配置文件/etc/ld.so.conf所列的目录.    
     
  (3)   -N   :   此选项指示ldconfig不重建缓存文件(/etc/ld.so.cache).若未用-X选项,ldconfig照常更新文件的连接.    
     
  (4)   -X   :   此选项指示ldconfig不更新文件的连接.若未用-N选项,则缓存文件正常更新.    
     
  (5)   -f   CONF   :   此选项指定动态链接库的配置文件为CONF,系统默认为/etc/ld.so.conf.    
     
  (6)   -C   CACHE   :   此选项指定生成的缓存文件为CACHE,系统默认的是/etc/ld.so.cache,此文件存放已排好序的可共享的动态链接库的列表.    
     
  (7)   -r   ROOT   :   此选项改变应用程序的根目录为ROOT(是调用chroot函数实现的).选择此项时,系统默认的配置文件/etc/ld.so.conf,实际对应的为ROOT/etc/ld.so.conf.如用-r   /usr/zzz时,打开配置文件/etc/ld.so.conf时,实际打开的是/usr/zzz/etc/ld.so.conf文件.用此选项,可以大大增加动态链接库管理的灵活性.    
     
  (8)   -l   :   通常情况下,ldconfig搜索动态链接库时将自动建立动态链接库的连接.选择此项时,将进入专家模式,需要手工设置连接.一般用户不用此项.    
     
  (9)   -p或--print-cache   :   此选项指示ldconfig打印出当前缓存文件所保存的所有共享库的名字.    
     
  (10)   -c   FORMAT   或   --format=FORMAT   :   此选项用于指定缓存文件所使用的格式,共有三种:old(老格式),new(新格式)和compat(兼容格式,此为默认格式).    
     
  (11)   -V   :   此选项打印出ldconfig的版本信息,而后退出.    
     
  (12)   -?   或   --help   或   --usage   :   这三个选项作用相同,都是让ldconfig打印出其帮助信息,而后退出.    
   
  如:  
  #   ldconfig   /usr/zhsoft/lib    
  #    
     
  注:   当用户在某个目录下面创建或拷贝了一个动态链接库,若想使其被系统共享,可以执行一下"ldconfig   目录名"这个命令.此命令的功能在于让ldconfig将指定目录下的动态链接库被系统共享起来,意即:在缓存文件/etc/ld.so.cache中追加进指定目录下的共享库.本例让系统共享了/usr/zhsoft/lib目录下的动态链接库. 

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
### 如何重置 `ld.so.conf` 文件 在 Linux 系统中,`ld.so.conf` 是用于配置动态链接库的重要文件之一。如果需要重置该文件的内容或恢复其默认状态,可以通过以下方法实现: #### 方法一:备份并清空现有文件 1. **创建备份** 在操作前建议先备份原始的 `/etc/ld.so.conf` 文件以防数据丢失: ```bash sudo cp /etc/ld.so.conf /etc/ld.so.conf.backup ``` 2. **清除原有内容** 使用编辑器打开文件并将其中的内容删除,或者直接覆盖为空文件: ```bash echo "" | sudo tee /etc/ld.so.conf ``` 3. **更新缓存** 修改完成后运行 `ldconfig` 更新动态链接库缓存文件 `/etc/ld.so.cache`[^1]: ```bash sudo ldconfig ``` #### 方法二:还原为系统默认值 某些发行版会在安装过程中提供默认的 `/etc/ld.so.conf` 文件模板。可以尝试从系统的包管理工具重新获取默认配置。 1. 对于基于 Debian 的系统(如 Ubuntu),可以从软件包中提取默认文件: ```bash sudo apt-get install --reinstall libc6 ``` 2. 如果存在额外的子目录配置文件(通常位于 `/etc/ld.so.conf.d/`),也可以清理这些文件夹内的自定义条目,并保留仅有的必要项[^5]。 #### 方法三:手动重建基本配置 如果没有现成的默认版本可用,则可以根据常见需求手工构建基础配置。例如,在大多数情况下只需要加入标准路径即可满足一般用途: ```plaintext /usr/local/lib /opt/mylibs ``` 保存修改后的文件后记得再次调用 `sudo ldconfig` 来生效新的设定[^4]。 --- ### 注意事项 - 调整此文件可能影响整个系统的共享库解析行为,请谨慎处理。 - 推荐优先利用环境变量 `LD_LIBRARY_PATH` 或者 `-Wl,-rpath` 编译选项作为临时解决方案而非频繁改动全局性的 `ld.so.conf` 设置[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值