构建后我无法轻易测试我的程序:
$ ./src/rap/rap -h
/home/il/workspace/rap/src/rap/.libs/lt-rap: error while loading shared libraries: libpmclient.so: cannot open shared object file: No such file or directory
libpmclient.so
位于 ./src/nsreclient/x86_64-unknown-linux-gnu/
中,正确的 -L
标志是链接时使用
Automake 使用 libtool 构建我的二进制文件。它创建一个包装器脚本 src/rap/rap
。
我的预期是:在生成脚本时,libtool 会忽略 LD_LIBRARY_PATH
的当前值。运行脚本时,它会将构建树中所需的目录附加到 LD_LIBRARY_PATH
的现有值。例如,执行:
LD_LIBRARY_PATH=/foo ./src/rap/rap
结果:
LD_LIBRARY_PATH=/foo:/home/il/workspace/rap/src/nsreclient/x86_64-unknown-linux-gnu ./src/rap/.libs/lt-rap
但 libtool 的作用恰恰相反:它将 LD_LIBRARY_PATH
的当前值硬编码到生成的脚本中:
LD_LIBRARY_PATH=/opt/oracle/product/11.2.0/client_64/lib:/lib:/usr/lib:/usr/local/lib; export LD_LIBRARY_PATH;
没有意义。为什么它不在那里添加构建树目录?
这里是 make 命令的格式化输出:
/bin/sh ../../libtool --tag=CC --mode=link gcc -Werror-implicit-function-declaration -O2 \
-pthread -o rap rap.o \
librap.a \
../liclib/liblic.a \
../fget-1.3.3/lib/libfget.a \
../lib_mt/libppb.la \
../sgplib/libsgp.la \
../../variations/asn1lib/ebuf_lib_mt/libasn1cebuf.la \
-L../../src/nsreclient/x86_64-unknown-linux-gnu -lpmclient \
-L/opt/oracle/product/11.2.0/client_64/lib -lclntsh \
-L/usr/lib -lexpat \
-lssl \
-lcrypto \
-lm
libtool: link: gcc -Werror-implicit-function-declaration -O2 -pthread -o .libs/rap rap.o librap.a ../liclib/liblic.a ../fget-1.3.3/lib/libfget.a \
../lib_mt/.libs/libppb.so \
../sgplib/.libs/libsgp.so \
../../variations/asn1lib/ebuf_lib_mt/.libs/libasn1cebuf.so \
-L../../src/nsreclient/x86_64-unknown-linux-gnu -lpmclient \
-L/opt/oracle/product/11.2.0/client_64/lib -lclntsh \
-L/usr/lib /usr/lib/libexpat.so \
-lssl -lcrypto -lm -pthread -Wl,-rpath -Wl,/usr/local/lib
最佳答案
包装器脚本根本不设置 LD_LIBRARY_PATH
。它假定包含所需库的子目录已添加到实际二进制文件的 RPATH
。
当-lfoo
标志传递给libtool
时,它首先尝试打开相应的文件libfoo.la
。如果该文件不存在,libtool
会退回到将标志传递给 gcc
而不改变。
如果 .la
文件存在并指向共享库,libtool
告诉链接器将包含该库的目录添加到 RPATH
输出二进制文件。
许多软件包将它们的 .la
文件安装到 /usr/lib/
。这意味着较新的 libtool
将能够解析由以前版本创建的文件。为预编译的.so
文件手动编写.la
文件而不是每次都生成它们一定是完全安全的。
我创建了文件 ./src/nsreclient/x86_64-unknown-linux-gnu/libpmclient.la
:
# libpmclient.la - a libtool library file
# Generated by ltmain.sh - GNU libtool 1.5.22 (1.1220.2.365 2005/12/18 22:14:06)
#
# Please DO NOT delete this file!
# It is necessary for linking the library.
# The name that we can dlopen(3).
dlname='libpmclient.so'
# Names of this library.
library_names='libpmclient.so'
# The name of the static archive.
old_library='libpmclient.a'
# Linker flags that cannot go in dependency_libs.
inherited_linker_flags=' -pthread'
# Libraries that this one depends upon.
dependency_libs=''
# Names of additional weak libraries provided by this library
weak_library_names=''
# Version information for libpmclient.
current=0
age=0
revision=0
# Is this an already installed library?
installed=no
# Should we warn about portability when linking against -modules?
shouldnotlink=no
# Files to dlopen/dlpreopen
dlopen=''
dlpreopen=''
# Directory that this library needs to be installed in:
libdir='/usr/local/lib'
重建后,我的包装器脚本不再报错。
关于c - 构建的库目录未添加到 libtool 包装器脚本中的 LD_LIBRARY_PATH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34397023/