4

我在办公室使用 libevent 2.X。在家里我有提供 libevent 1.4 的 Ubuntu 10.4,所以我下载并编译了最新版本的 libevent。现在我在 /usr/local/include 中有头文件,在 /usr/local/lib 中有库文件。但我的问题是可执行文件似乎没有找到正确的库。

我试过这个

vickey@home:~/development/Server/test$ g++ Server.cpp -L /usr/local/lib -levent -I /usr/local/include/

vickey@home:~/development/Server/test$ ./a.out 
./a.out: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory

编译的详细输出如下

vickey@home:~/development/Server/test$ g++ Server.cpp -L /usr/local/lib -levent -I /usr/local/include/ -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) 
COLLECT_GCC_OPTIONS='-L' '/usr/local/lib' '-I' '/usr/local/include/' '-v' '-shared-libgcc' '-mtune=generic' '-march=i486'
 /usr/lib/gcc/i486-linux-gnu/4.4.3/cc1plus -quiet -v -I /usr/local/include/ -D_GNU_SOURCE Server.cpp -D_FORTIFY_SOURCE=2 -quiet -dumpbase Server.cpp -mtune=generic -march=i486 -auxbase Server -version -fstack-protector -o /tmp/cceeQ3Uc.s
GNU C++ (Ubuntu 4.4.3-4ubuntu5) version 4.4.3 (i486-linux-gnu)
    compiled by GNU C version 4.4.3, GMP version 4.3.2, MPFR version 2.4.2-p1.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/i486-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../i486-linux-gnu/include"
ignoring nonexistent directory "/usr/include/i486-linux-gnu"
ignoring duplicate directory "/usr/local/include/"
  as it is a non-system directory that duplicates a system directory
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/4.4
 /usr/include/c++/4.4/i486-linux-gnu
 /usr/include/c++/4.4/backward
 /usr/local/include
 /usr/lib/gcc/i486-linux-gnu/4.4.3/include
 /usr/lib/gcc/i486-linux-gnu/4.4.3/include-fixed
 /usr/include
End of search list.
GNU C++ (Ubuntu 4.4.3-4ubuntu5) version 4.4.3 (i486-linux-gnu)
    compiled by GNU C version 4.4.3, GMP version 4.3.2, MPFR version 2.4.2-p1.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 35224f2c24023afb0a5be7befe8d5f3f
COLLECT_GCC_OPTIONS='-L' '/usr/local/lib' '-I' '/usr/local/include/' '-v' '-shared-libgcc' '-mtune=generic' '-march=i486'
 as -V -Qy -o /tmp/cclpymAs.o /tmp/cceeQ3Uc.s
GNU assembler version 2.20.1 (i486-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.20.1-system.20100303
COMPILER_PATH=/usr/lib/gcc/i486-linux-gnu/4.4.3/:/usr/lib/gcc/i486-linux-gnu/4.4.3/:/usr/lib/gcc/i486-linux-gnu/:/usr/lib/gcc/i486-linux-gnu/4.4.3/:/usr/lib/gcc/i486-linux-gnu/:/usr/lib/gcc/i486-linux-gnu/4.4.3/:/usr/lib/gcc/i486-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/i486-linux-gnu/4.4.3/:/usr/lib/gcc/i486-linux-gnu/4.4.3/:/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../:/lib/:/usr/lib/:/usr/lib/i486-linux-gnu/
COLLECT_GCC_OPTIONS='-L' '/usr/local/lib' '-I' '/usr/local/include/' '-v' '-shared-libgcc' '-mtune=generic' '-march=i486'
 /usr/lib/gcc/i486-linux-gnu/4.4.3/collect2 --build-id --eh-frame-hdr -m elf_i386 --hash-style=both -dynamic-linker /lib/ld-linux.so.2 -z relro /usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o /usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crti.o /usr/lib/gcc/i486-linux-gnu/4.4.3/crtbegin.o -L /usr/local/lib -L/usr/lib/gcc/i486-linux-gnu/4.4.3 -L/usr/lib/gcc/i486-linux-gnu/4.4.3 -L/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/i486-linux-gnu/4.4.3/../../.. -L/usr/lib/i486-linux-gnu /tmp/cclpymAs.o -levent -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/i486-linux-gnu/4.4.3/crtend.o /usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crtn.o
4

1 回答 1

6

在运行时,动态可执行文件由动态链接器再次链接ld.so。正如您必须在编译时传递-L/usr/local/libgcc链接器以找到您的库一样,您必须告诉 ld.so 您不寻常的搜索路径,无论是通过 ld.so.conf 永久还是通过环境变量 LD_LIBRARY_PATH,例如

LD_LIBRARY_PATH=/usr/local/lib ./a.out

TL;手册的 DR:ldd将为您概述动态链接器将执行的操作,并且不要忘记ldconfig在更改ld.so.conf.

于 2012-08-17T06:49:24.423 回答