boost 和 cpp-netlib 使编译错误
[已解决] 创建了从 /usr/lib/lib/* 到 /usr/lib* 的符号链接
[更新 3] 新版本:
好吧,我想我修复了一些
使用 find / -name " libboost_system.*"
outout 是
/usr/include/boost/lib/libboost_system.so
/usr/include/boost/lib/libboost_system.a
/usr/include/boost/lib/libboost_system.so.1.46.1
/usr/lib/lib/libboost_system.so
/usr/lib/lib/libboost_system.a
/usr/lib/lib/libboost_system.so.1.46.1
/usr/local/include/boost_1_46_1/bin.v2/libs/system/build/gcc-4.4.3/release/link-static/threading-multi/libboost_system.a
/usr/local/include/boost_1_46_1/bin.v2/libs/system/build/gcc-4.4.3/release/threading-multi/libboost_system.so.1.46.1
/usr/local/lib/libboost_system.so
/usr/local/lib/libboost_system.a
/usr/local/lib/libboost_system.so.1.46.1
/root/tmp/boost_1_46_1/bin.v2/libs/system/build/gcc-4.4.3/release/link-static/threading-multi/libboost_system.a
/root/tmp/boost_1_46_1/bin.v2/libs/system/build/gcc-4.4.3/release/threading-multi/libboost_system.so.1.46.1
为什么这些文件位于 /usr/lib/lib
中?这是一个问题吗?
和 ls -l /usr/lib/lib | grep boost_system
ls -l /usr/lib/lib | grep boost_system
-rw-r--r-- 1 root root 21574 2011-05-09 15:15 libboost_system.a
lrwxrwxrwx 1 root root 25 2011-05-09 15:15 libboost_system.so -> libboost_system.so.1.46.1
-rwxr-xr-x 1 root root 20053 2011-05-09 15:15 libboost_system.so.1.46.1
atm 我的 makefile 看起来像
LIBPATH=-I/usr/local/include/cpp-netlib
LIBS=$(LIBPATH) -lboost_system -lboost_filesystem -lboost_thread -lpthread
LD=g++ -g
CPP=g++ -c -g $(LIBS)
P=.
OBJ=$(P)/tmp/main.o $(P)/tmp/CLink.o $(P)/tmp/CFetcher.o
main: $(OBJ); $(LD) $(OBJ) $(LIBS) -o $@
$(P)/tmp/CLink.o: $(P)/src/CLink.cpp $(P)/include/CLink.h; $(CPP) -c $< -o $@
$(P)/tmp/CFetcher.o: $(P)/src/CFetcher.cpp $(P)/include/CFetcher.h; $(CPP) -c $< -o $@
$(P)/tmp/main.o: $(P)/src/main.cpp $(P)/include/CLink.h $(P)/include/CFetcher.h ; $(CPP) -c $< -o $@
all:
touch $(P)/tmp/*.o;
touch main;
rm -f $(P)/tmp/*.o;
rm -f main;
make main;
编译器输出是谎言
g++ -c -g -I/usr/local/include/cpp-netlib -lboost_system -lboost_filesystem -lboost_thread -lpthread -c src/main.cpp -o tmp/main.o
g++ -c -g -I/usr/local/include/cpp-netlib -lboost_system -lboost_filesystem -lboost_thread -lpthread -c src/CLink.cpp -o tmp/CLink.o
g++ -c -g -I/usr/local/include/cpp-netlib -lboost_system -lboost_filesystem -lboost_thread -lpthread -c src/CFetcher.cpp -o tmp/CFetcher.o
g++ -g ./tmp/main.o ./tmp/CLink.o ./tmp/CFetcher.o -I/usr/local/include/cpp-netlib -lboost_system -lboost_filesystem -lboost_thread -lpthread -o main
所以对我来说一切看起来都不错但是当我尝试运行程序时
./main
./main: error while loading shared libraries: libboost_system.so.1.46.1: cannot open shared object file: No such file or directory
[SOLVED] created symlinks from /usr/lib/lib/* to /usr/lib*
[UPDATE 3] NEW VERSION:
Ok, I think I fixed something
use find / -name "libboost_system.*"
outout was
/usr/include/boost/lib/libboost_system.so
/usr/include/boost/lib/libboost_system.a
/usr/include/boost/lib/libboost_system.so.1.46.1
/usr/lib/lib/libboost_system.so
/usr/lib/lib/libboost_system.a
/usr/lib/lib/libboost_system.so.1.46.1
/usr/local/include/boost_1_46_1/bin.v2/libs/system/build/gcc-4.4.3/release/link-static/threading-multi/libboost_system.a
/usr/local/include/boost_1_46_1/bin.v2/libs/system/build/gcc-4.4.3/release/threading-multi/libboost_system.so.1.46.1
/usr/local/lib/libboost_system.so
/usr/local/lib/libboost_system.a
/usr/local/lib/libboost_system.so.1.46.1
/root/tmp/boost_1_46_1/bin.v2/libs/system/build/gcc-4.4.3/release/link-static/threading-multi/libboost_system.a
/root/tmp/boost_1_46_1/bin.v2/libs/system/build/gcc-4.4.3/release/threading-multi/libboost_system.so.1.46.1
why are these files in /usr/lib/lib
? and is it a problem ?
and the ls -l /usr/lib/lib | grep boost_system
ls -l /usr/lib/lib | grep boost_system
-rw-r--r-- 1 root root 21574 2011-05-09 15:15 libboost_system.a
lrwxrwxrwx 1 root root 25 2011-05-09 15:15 libboost_system.so -> libboost_system.so.1.46.1
-rwxr-xr-x 1 root root 20053 2011-05-09 15:15 libboost_system.so.1.46.1
atm my makefile looks like
LIBPATH=-I/usr/local/include/cpp-netlib
LIBS=$(LIBPATH) -lboost_system -lboost_filesystem -lboost_thread -lpthread
LD=g++ -g
CPP=g++ -c -g $(LIBS)
P=.
OBJ=$(P)/tmp/main.o $(P)/tmp/CLink.o $(P)/tmp/CFetcher.o
main: $(OBJ); $(LD) $(OBJ) $(LIBS) -o $@
$(P)/tmp/CLink.o: $(P)/src/CLink.cpp $(P)/include/CLink.h; $(CPP) -c lt; -o $@
$(P)/tmp/CFetcher.o: $(P)/src/CFetcher.cpp $(P)/include/CFetcher.h; $(CPP) -c lt; -o $@
$(P)/tmp/main.o: $(P)/src/main.cpp $(P)/include/CLink.h $(P)/include/CFetcher.h ; $(CPP) -c lt; -o $@
all:
touch $(P)/tmp/*.o;
touch main;
rm -f $(P)/tmp/*.o;
rm -f main;
make main;
The Compiler output is lie
g++ -c -g -I/usr/local/include/cpp-netlib -lboost_system -lboost_filesystem -lboost_thread -lpthread -c src/main.cpp -o tmp/main.o
g++ -c -g -I/usr/local/include/cpp-netlib -lboost_system -lboost_filesystem -lboost_thread -lpthread -c src/CLink.cpp -o tmp/CLink.o
g++ -c -g -I/usr/local/include/cpp-netlib -lboost_system -lboost_filesystem -lboost_thread -lpthread -c src/CFetcher.cpp -o tmp/CFetcher.o
g++ -g ./tmp/main.o ./tmp/CLink.o ./tmp/CFetcher.o -I/usr/local/include/cpp-netlib -lboost_system -lboost_filesystem -lboost_thread -lpthread -o main
So for me all looks nice but when i try to run the program
./main
./main: error while loading shared libraries: libboost_system.so.1.46.1: cannot open shared object file: No such file or directory
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
-l
标志必须位于链接器命令行上的源文件之后。是的,这意味着您必须将
LD
定义拆分为LD
和LIBS
,将所有-L
和-l
标志,并将链接命令更改为:$(LD) $(OBJ) $(LIBS) -o $@
库 (
.so< /code>(动态)或
.a
(静态))文件必须与标头版本相同。虽然/usr/local/include/boost_1_46_1/
中安装了 boost 1.46.1 标头,但相应的库文件似乎根本没有安装。唯一安装的库是/usr/lib
中的版本 1.40.0,因此链接器会找到这些库(默认情况下会搜索/usr/lib
即使您没有包括-L/usr/lib
标志),但它们不包含 1.46.1 所期望的符号。请注意,当链接到共享库时(在 Linux 中强烈建议使用共享库),链接器会查找扩展名为
.so
的文件,但这通常是指向具有添加版本的文件的符号链接后缀,链接器读取它并将目标名称记录在二进制文件中。这样,针对 .1.40 编译的程序将在安装 1.46 后继续工作,因为libboost*.so.1.40.0
可能(并且必须)在.so< 之后保留/code> 重定向到 1.46.1 版本。
甚至应该可以像这样安装:
并使用
-L/usr/local/lib/boost_1_46_1
进行编译,尽管我目前找不到任何可以确认这一点的包。这样,您就可以安装多个版本的开发文件,并使用显式的-I
和-L
标志在它们之间切换,而动态链接器仍然会找到运行时文件,为此只查看/usr/local/lib
、/usr/lib
和/lib
(可以在/etc/ 中配置) ld.so.conf
,但这是默认设置)。The
-l
flags must come after the source files on linker command-line.Yes, that means you'll have to split the
LD
definition toLD
andLIBS
, put all the-L
and-l
flags in the later and change the link command to:$(LD) $(OBJ) $(LIBS) -o $@
The library (
.so
(dynamic) or.a
(static)) files have to be the same version as the headers. While there are boost 1.46.1 headers installed in/usr/local/include/boost_1_46_1/
, the corresponding library files don't seem to be installed at all. The only installed libraries are version 1.40.0 in/usr/lib
, so the linker finds those (/usr/lib
would be searched by default even if you didn't include the-L/usr/lib
flag), but they don't contain the symbols expected by 1.46.1.Note that when linking against shared library (using shared libraries is strongly recommended in Linux), the linker looks for the file with
.so
extension, but that is usually symlink to a file with added version suffix and the linker reads it and records the target name in the binary. That way programs compiled against the .1.40 will continue to work when 1.46 is installed, because thelibboost*.so.1.40.0
may (and have to) stay around after the.so
is redirected to the 1.46.1 version.It should be even possible to install like:
and compile using
-L/usr/local/lib/boost_1_46_1
, though I currently can't find any package that would do it to confirm this. This way you could have development files for multiple versions installed and switch between them using explicit-I
and-L
flags while dynamic linker would still find the runtime files, for which it only looks in/usr/local/lib
,/usr/lib
and/lib
(it can be configured in/etc/ld.so.conf
, but that's the default).使用 strace 执行您的应用程序。这将显示您的应用程序正在寻找您的 boost 库的位置。就我而言,应用程序正在
/usr/lib/x86_64-linux-gnu
中查找实际位置为/usr/lib/lib
的 boost 库。在我的例子中,一个简单的导出语句添加了 boost 共享库的路径LD_LIBRARY_PATH=/usr/lib/lib
,效果很好。strace 的输出
Execute your application with strace. This will show the location were you app is looking for your boost libs. In my case an app was looking in
/usr/lib/x86_64-linux-gnu
for boost libs where the actual location was/usr/lib/lib
. A simple export statement adding the paths for the boost shared libs in my caseLD_LIBRARY_PATH=/usr/lib/lib
worked a treat.output from strace