简体   繁体   English

C ++链接Boost库

[英]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 . 首先,我构建了需要构建的Boost库,方法是转到/usr/local/boost_1_49_0/并运行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 步骤(1)在/usr/local/boost_1_49_0/stage/lib创建了所有.so.a文件
  3. I tested linking against a library, say lboost_regex and #include <boost/regex> in my source code. 我测试了针对库的链接,在我的源代码中说lboost_regex#include <boost/regex> This also went off alright. 这也没问题。
  4. Finally trying out the example on asio, I tried: 最终尝试了asio上的示例,我尝试了:

     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. (4)编译好了。 But when I run the program with ./MAIN , I get the following error: 但是,当我使用./MAIN运行程序时,出现以下错误:

./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; -L选项仅设置一个编译时库搜索路径; 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; g++进行设置的一种方法是通过编译器将-rpath传递给链接器。 in your case you could say -Wl,-rpath -Wl,/usr/local/boost_1_49_0/stage/lib . 在您的情况下,您可以说-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). 另一种方法是将库安装在链接程序默认搜索的位置(例如, /usr/local/lib可能是这样的位置,具体取决于系统的配置方式)。

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. 还有一种方法是设置环境变量,例如LD_LIBRARY_PATH (Linux或Solaris)或DYLD_LIBRARY_PATH (Mac OS X),以告诉链接器从设置了该变量的Shell启动可执行文件时在何处搜索。

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. 这是一个链接,其中包含有关加载程序的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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