例如,有个so在/tmp/libs/libzmq.so.3:
[winlin@dev6 libs]$ ls /tmp/libs/ -lh
total 2.5M
lrwxrwxrwx 1 winlin winlin 15 Dec 12 12:58 libzmq.so -> libzmq.so.3.0.0
lrwxrwxrwx 1 winlin winlin 15 Dec 12 12:58 libzmq.so.3 -> libzmq.so.3.0.0
-rwxrwxr-x 1 winlin winlin 2.5M Dec 12 12:49 libzmq.so.3.0.0
#include
#include "/home/winlin/zeromq-3.2.2/include/zmq.h"
/**
g++ RRServer.cpp -L/tmp/libs -lzmq -g -O0 -o RRServer
*/
int main(int argc, char** argv){
void* context = zmq_ctx_new();
void* responder = zmq_socket(context, ZMQ_REP);
zmq_bind(responder, "tcp://*:1990");
while(1){
zmq_msg_t request;
zmq_msg_init(&request);
zmq_msg_recv(&request, responder, 0);
zmq_msg_close(&request);
zmq_msg_t reply;
zmq_msg_init_size(&reply, 6);
memcpy(zmq_msg_data(&reply), "World", 6);
zmq_msg_send(&reply, responder, 0);
zmq_msg_close(&reply);
}
return 0;
}
[winlin@dev6 zeromq]$ g++ RRServer.cpp -L/tmp/libs -lzmq -g -O0 -o RRServer
[winlin@dev6 zeromq]$ ldd RRServer
linux-vdso.so.1 => (0x00007fff7bdff000)
libzmq.so.3 => not found
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x0000003730000000)
libm.so.6 => /lib64/libm.so.6 (0x0000003723800000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x000000372e400000)
libc.so.6 => /lib64/libc.so.6 (0x0000003723400000)
/lib64/ld-linux-x86-64.so.2 (0x0000003722c00000)
1. 设置LD_LIBRARY_PATH,系统会在这个路径下去找特殊的so:
[winlin@dev6 zeromq]$ export LD_LIBRARY_PATH=/tmp/libs:$LD_LIBRARY_PATH
[winlin@dev6 zeromq]$ ldd RRServer
linux-vdso.so.1 => (0x00007fff987ff000)
libzmq.so.3 => /tmp/libs/libzmq.so.3 (0x00007f9b67648000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x0000003730000000)
libm.so.6 => /lib64/libm.so.6 (0x0000003723800000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x000000372e400000)
libc.so.6 => /lib64/libc.so.6 (0x0000003723400000)
librt.so.1 => /lib64/librt.so.1 (0x0000003724800000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003724000000)
/lib64/ld-linux-x86-64.so.2 (0x0000003722c00000)
还有其他的方法。
2. 编译时指定路径,这个对自己的so好使,但对libzmq不太好使:
[winlin@dev6 shared-libs]$ g++ shared-lib-doo.cpp -g -O0 -shared -fPIC -o libdoo.so
[winlin@dev6 shared-libs]$ g++ shared-lib.cpp ./libfoo.so ./libdoo.so -g -O0 -o shared-lib
[winlin@dev6 shared-libs]$ ldd shared-lib
linux-vdso.so.1 => (0x00007fff252df000)
./libfoo.so (0x00007f9f7df9c000)
./libdoo.so (0x00007f9f7dd9a000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x0000003730000000)
libm.so.6 => /lib64/libm.so.6 (0x0000003723800000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x000000372e400000)
libc.so.6 => /lib64/libc.so.6 (0x0000003723400000)
/lib64/ld-linux-x86-64.so.2 (0x0000003722c00000)
4. 使用工具,修改编译出来的执行文件的路径,这个好使:
http://nixos.org/patchelf.html
http://hydra.nixos.org/build/1524660/download/2/patchelf-0.6.tar.bz2
PatchELF is a small utility to modify the dynamic linker and RPATH of ELF executables.
Likewise, you can change the RPATH, the linker search path embedded into executables and dynamic libraries:
patchelf --set-rpath /opt/my-libs/lib:/foo/lib program
下载源码后,编译:
[winlin@dev6 patchelf-0.6]$ (cd /home/winlin/patchelf-0.6; ./configure ; make)
[winlin@dev6 patchelf-0.6]$ file /home/winlin/patchelf-0.6/src/patchelf
/home/winlin/patchelf-0.6/src/patchelf: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped
[winlin@dev6 patchelf-0.6]$ /home/winlin/patchelf-0.6/src/patchelf
syntax: /home/winlin/patchelf-0.6/src/patchelf
[--set-interpreter FILENAME]
[--print-interpreter]
[--set-rpath RPATH]
[--shrink-rpath]
[--print-rpath]
[--force-rpath]
[--debug]
[--version]
FILENAME
[winlin@dev6 zeromq]$ /home/winlin/patchelf-0.6/src/patchelf --set-rpath /tmp/libs RRServer
[winlin@dev6 zeromq]$ ldd RRServer
linux-vdso.so.1 => (0x00007ffff37b0000)
libzmq.so.3 => /tmp/libs/libzmq.so.3 (0x00007f0c49c10000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x0000003730000000)
libm.so.6 => /lib64/libm.so.6 (0x0000003723800000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x000000372e400000)
libc.so.6 => /lib64/libc.so.6 (0x0000003723400000)
librt.so.1 => /lib64/librt.so.1 (0x0000003724800000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003724000000)
/lib64/ld-linux-x86-64.so.2 (0x0000003722c00000)