2007年8月1日 星期三

執行程式時, 找不到shared library 時, 該如何處理

# ./aaad
./aaad: error while loading shared libraries: libosip2.so.4: cannot open shared object file: No such file or directory

遇到執行程式時, 找不到shared library 時, ㄧ般都是路徑沒有設定正確
可以透過修改 /etc/ld.so.conf,
include /etc/ld.so.conf.d/*.conf
/usr/X11R6/lib
/usr/local/lib <--- 增加這一行 然後執行 # ldconfig 就會做出動態連結所需的cache檔 或是直接執行下面這一行也可以 export LD_LIBRARY_PATH=/usr/local/lib 另外也可以直接在程式run time 來設定環境變數, 直接執行程式 # ./aaa ./aaa: relocation error: ./aaa: undefined symbol: pcap_findalldevs ===> 因為預設的shared library 只有找/usr/lib, 沒有/usr/local/lib, 所以若沒有設定環境變數, 就無法載入 shared library, 無法成功執行
# LD_LIBRARY_PATH=/usr/local/lib ./aaa
ok
==> 有另外設定shared library path, 就可以找到 shared library, 就可以成功執行

標準做法如下
Libraries have been installed in:
/usr/lib

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath=LIBDIR' linker flag
e.g.
# gcc -c pq.c
# gcc -lpq -L/usr/local/lib -Wl,rpath=/usr/local/lib -o pq pq.o

- have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.

實際範例
test -z "/usr/lib" || mkdir -p -- "/usr/lib"
/bin/sh ../libtool --mode=install /usr/bin/install -c 'libeXosip2.la' '/usr/lib/libeXosip2.la'
/usr/bin/install -c .libs/libeXosip2.so.6.0.0 /usr/lib/libeXosip2.so.6.0.0
(cd /usr/lib && { ln -s -f libeXosip2.so.6.0.0 libeXosip2.so.6 || { rm -f libeXosip2.so.6 && ln -s libeXosip2.so.6.0.0 libeXosip2.so.6; }; })
(cd /usr/lib && { ln -s -f libeXosip2.so.6.0.0 libeXosip2.so || { rm -f libeXosip2.so && ln -s libeXosip2.so.6.0.0 libeXosip2.so; }; })
/usr/bin/install -c .libs/libeXosip2.lai /usr/lib/libeXosip2.la
/usr/bin/install -c .libs/libeXosip2.a /usr/lib/libeXosip2.a
chmod 644 /usr/lib/libeXosip2.a
ranlib /usr/lib/libeXosip2.a
PATH="$PATH:/sbin" ldconfig -n /usr/lib

結果如下
# ls -l /usr/lib/libe*
-rw-r--r-- 1 root root 632866 Dec 24 12:35 /usr/lib/libeXosip2.a
-rwxr-xr-x 1 root root 902 Dec 24 12:35 /usr/lib/libeXosip2.la
lrwxrwxrwx 1 root root 19 Dec 24 12:35 /usr/lib/libeXosip2.so -> libeXosip2.so.6.0.0
lrwxrwxrwx 1 root root 19 Dec 24 12:35 /usr/lib/libeXosip2.so.6 -> libeXosip2.so.6.0.0
-rwxr-xr-x 1 root root 544903 Dec 24 12:35 /usr/lib/libeXosip2.so.6.0.0

ortp 的make install範例
test -z "/usr/lib" || mkdir -p -- "/usr/lib"
/bin/sh ../libtool --mode=install /usr/bin/install -c 'libortp.la' '/usr/lib/libortp.la'
/usr/bin/install -c .libs/libortp.so.5.0.0 /usr/lib/libortp.so.5.0.0
(cd /usr/lib && { ln -s -f libortp.so.5.0.0 libortp.so.5 || { rm -f libortp.so.5 && ln -s libortp.so.5.0.0 libortp.so.5; }; })
(cd /usr/lib && { ln -s -f libortp.so.5.0.0 libortp.so || { rm -f libortp.so && ln -s libortp.so.5.0.0 libortp.so; }; })
/usr/bin/install -c .libs/libortp.lai /usr/lib/libortp.la
/usr/bin/install -c .libs/libortp.a /usr/lib/libortp.a
chmod 644 /usr/lib/libortp.a
ranlib /usr/lib/libortp.a
PATH="$PATH:/sbin" ldconfig -n /usr/lib
----------------------------------------------------------------------
Libraries have been installed in:
/usr/lib

沒有留言: