运行时有多个版本的库文件怎么办?

1. The linker is able to accept filenames too

gcc  app.o -l:libmy.so.1 -o app

From man ld:

-l namespec
--library=namespec

Add the archive or object file specified by namespec to the list of files to link. This option may be used any number of times. If namespec is of the form :filename, ld will search the library path for a file called filename, otherwise it will search the library path for a file called libnamespec.a.

I noticed that older versions do not support it, so check man ld -l or --library option on your system.

You could also link to the file mentioning its full name

gcc  app.o /mylibpath/libmy.so.1 -o app
 

编译完成后可以使用以下命令查看路径是否设置成功了

[plain] view plain copy
  1. $ readelf -dl  evh  

看到类似下面的信息则是路径设置成功了

[plain] view plain copy
  1. 0x000000000000000f (RPATH)              Library rpath: [/usr/local/lib/:/data1/thd/jsoncpp/lib/:/data1/thd/leveldb/lib/:/data1/tools/boost_1_53_0/stage/lib/]  

另外以下命令可以查看可执行文件的依赖库

[plain] view plain copy
  1. $ ldd a.out  

可明确的知道需要那些依赖库,以及哪些依赖库找不到



2.    -Wl,-rpath,  用于指定程序运行时查找动态链接库的路径,多个路径是使用冒号隔开。这样就不用添加路径到 /etc/ld.so.conf 文件中了,在需要多个so版本共存时很有用


3. 命令

I don’t know about the specific application, but sometimes it’s indeed necessary to set in a small wrapper script to the application an additional LD_LIBRARY_PATH before starting the application. Which library will be used can be checked by:

Code:
$ ldd program
Another variable you can try is LD_PRELOAD if it’s still using the wrong libraries.

Another approach to make it more happen automatically for you: install the new libraries in a different location (i.e. go back to the initial setup of the libraries in your system). Then you can change the rpath to the libraries directly in the compiled application by the tool:
Code:
$ chrpath program
which can change the written rpath to libraries during the link step of the application. You can check the binary whether it was changed correctly by:
Code:
$ readelf -d program
If there was no rpath compiled in, you can also add the appropriate option already to the link step.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值