简体   繁体   中英

C++ linking boost library

  1. First I built the Boost libraries that require building by going to /usr/local/boost_1_49_0/ and running the bootstrap.sh . This went off alright.
  2. Step (1) created all the .so and .a files in /usr/local/boost_1_49_0/stage/lib
  3. I tested linking against a library, say lboost_regex and #include <boost/regex> in my source code. This also went off alright.
  4. Finally trying out the example on asio, I tried:

     g++ -I/usr/local/boost_1_49_0 MAIN.cpp -o MAIN -L/usr/local/boost_1_49_0/stage/lib -lboost_thread -lboost_system -lpthread 

(4) compiled alright. But when I run the program with ./MAIN , I get the following error:

./MAIN: error while loading shared libraries: libboost_system.so.1.49.0: cannot open shared object file: No such file or directory 

The -L option only sets a compile-time library search path; if you want a shared library to be found at runtime then its directory must be known at runtime.

One way to set this with g++ is to pass -rpath to the linker, via the compiler; in your case you could say -Wl,-rpath -Wl,/usr/local/boost_1_49_0/stage/lib . (This embeds the directory in the executable.)

Another way is to install the libraries in a place that the linker searches by default (eg /usr/local/lib might be such a place, depending on how the system is configured).

Yet another way is to set an environment variable such as LD_LIBRARY_PATH (Linux or Solaris) or DYLD_LIBRARY_PATH (Mac OS X), to tell the linker where to search when launching executables from the shell where the variable is set.

Are you sure the shared library is in a place where the loader can find it? Either put it in a system wide directory or the same directory as the executable.

Here's a link with more information about the loader.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM