问题描述
我在编译包含"boost/asio.hpp"的程序时遇到问题.
编译该程序(来自boost网站):
I'm having problems with compiling a program which includes "boost/asio.hpp".
Compiling this program(taken from boost site):
example.cpp:
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(
in(std::cin), in(), std::cout << (_1 * 3) << " " );
}
与
c++ -I path/to/boost_1_55_0 example.cpp -o example
正常工作.但是当程序包括:
works fine.But when the program includes:
boost/asio.hpp
我正在尝试使用:
g++ -I /usr/local/boost_1_55_0 example.cpp -o example -lboost_system -lboost_thread
生成了一个可执行文件,但是在尝试执行示例"时出现此错误:
an executable is generated ,but I'm getting this error when trying to execute "example":
./example: error while loading shared libraries: libboost_system.so.1.55.0: cannot open shared object file: No such file or directory
文件"libboost_system.so.1.55.0"位于"/usr/local/lib".我也尝试用:
The file "libboost_system.so.1.55.0" is located at "/usr/local/lib".I also tried to compile the program with :
g++ -I /usr/local/boost_1_55_0 -L/usr/local/lib example.cpp -o example -lboost_system -lboost_thread
并遇到相同的错误.
我该如何解决?
推荐答案
您需要告诉链接器在哪里可以找到所需的库.我更喜欢使用RPATH:
You need to tell the linker where to find the library it needs. I prefer RPATH for this:
g++ -I /usr/local/boost_1_55_0 -Wl,-rpath=/usr/local/lib example.cpp -o example -lboost_system -lboost_thread
将/usr/local/lib
烘焙到可执行文件中,以便ld
稍后可以找到它.您可以在构建后运行ldd example
来查看ld
将加载的内容.我敢打赌,现在它说未找到",并且在添加RPATH之后将找到该库.
That bakes /usr/local/lib
into the executable so ld
can find it later. You can see what ld
will load by running ldd example
after building. I bet right now it says "not found" and after adding RPATH it will find the library.
另一种选择是将/usr/local/lib设置为/etc/ld.so.conf中的系统搜索路径,但这要重得多.
Another option is to set /usr/local/lib as a system search path in your /etc/ld.so.conf, but that's quite a bit more heavyweight.
这篇关于通过boost编译问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!