您的位置:首页 > 其它

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

2015-10-18 17:34 267 查看
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 plaincopy

$ readelf -dl  evh  

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

[plain]
view plaincopy

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 plaincopy

$ ldd a.out  

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

2.    -Wl,-rpath,  用于指定程序运行时查找动态链接库的路径,多个路径是使用冒号隔
4000
开。这样就不用添加路径到 /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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: