Compiling issues with boost

2019-05-28 18:31发布

问题:

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) << " " );
}

with

c++ -I path/to/boost_1_55_0 example.cpp -o example 

works fine. But when the program includes:

boost/asio.hpp

And I'm trying to compile it with:

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

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

And got the same error.

How can I fix this?

回答1:

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

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.

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.



回答2:

set up LD_LIBRARY_PATH as export LD_LIBRARY_PATH= path to boost