Program compiles fine with boost libs, but error w

2019-04-17 08:33发布

问题:

In a nutshell...

C++ program (using the boost libraries) compiles fine in Eclipse, but then “error while loading shared libraries: libboost_thread.so.1.46.1: cannot open shared object file: No such file or directory” it shown when running it.


Details

I am running a basic program on C++ to check that I can use the boost threading libraries correctly.

#include <boost/thread/thread.hpp>
#include <iostream>

void hello ()
{
    Std::cout<<”Hello, I am a thread”<<std::endl;
}

int main ()
{
    boost::thread th1(&hello);
    th1.join();
}

The code compiles fine, so I believed that I had installed and set up the boost libraries correctly (added directories to include etc)

However when I try to run the program I get the following error message in the consol

error while loading shared libraries: libboost_thread.so.1.46.1: cannot open shared object file: No such file or directory

回答1:

I had a very similar issue with FreeFileSYnc, compiles fine but won't launch due to a libboost thread error :

FreeFileSync: error while loading shared libraries: libboost_thread.so.1.51.0: cannot open shared object file: No such file or directory

To fix it I did :

sudo ldconfig /usr/local/lib

It may help other people.



回答2:

Have you checked if the libboost_thread.so.1.46.1 is present at LD_LIBRARY_PATH?
Copy the library at your LD_LIBRARY_PATH, this path is searched for dynamic libraries(so) at runtime.



回答3:

boost thread is a dynamic library. It must be found at runtime for the program to run (like a DLL in Windows).



回答4:

A bit late to the party, but hassled all day with the same problem. I recently installed the latest boost 1_51_0 locally into my $HOME/bin/boost_1_51_0. What worked for me, was to export the include and library path by adding these lines to my .bash_rc:

export CPP_INCLUDE_PATH=$HOME/bin/boost_1_51_0/include
export LD_LIBRARY_PATH=$HOME/bin/boost_1_51_0/lib:$LD_LIBRARY_PATH

Then add the include path and lib path to your Makefile:

INCLUDE_BOOST += -I$(HOME)/bin/boost_1_51_0/include
LDLIBS_BOOST  += -L$(HOME)/bin/boost_1_51_0/lib

CPPFLAGS += $(INCLUDE)
CPPFLAGS += $(INCLUDE_BOOST)

Then you can add the libs from 1_51 via

LDLIBS += -lm
LDLIBS += -lboost_filesystem
LDLIBS += -lboost_chrono
LDLIBS += -lboost_timer
LDLIBS += -lboost_system

$(TARGET): $(SRCS)
    $(CXX) $(CPPFLAGS) $(LDLIBS_BOOST) $(SRCS) $(LDLIBS)  $(OPT)  -o $@


回答5:

I had the same problem, and ldconfig did not fix it.

If you, as I did, installed the boost libraries using a plain bjam command, chances are you installed the libraries in a stage subfolder. See this page as a reference. What worked for me was to run the following:

sudo ldconfig /usr/local/include/boost_1_64_0/stage/lib