我已经安装了boost库。我正在使用Eclipse来创建一个简单的boost项目。
#include <stdio.h>
#include <boost/filesystem.hpp>
int main() {
boost::filesystem::path path("/Users/schoen"); // random pathname
bool result = boost::filesystem::is_directory(path);
printf("Path is a directory : %d\n", result);
return 0;
}
我已经在此项目的属性中设置了包含文件夹和库文件夹的路径。但我收到了运行时错误:dyld: Library not loaded: libboost_system.dylib
。为解决此问题,我转到run configuration
并将环境变量DYLD_LIBRARY_PATH
设置为/Users/myName/Documents/Softwares/boost_1_59_0/stage/lib
。这解决了我的问题。
我需要什么:我不想为每个基于Boost的项目设置environment variable
。因此,我尝试设置.bash_profile
。我在.bash_profile
中写了以下几行。
# Following lines are for Boost Library
DYLD_LIBRARY_PATH=/Users/myName/Documents/Softwares/boost_1_59_0/stage/lib
export DYLD_LIBRARY_PATH
问题:通过在.bash_profile
中设置变量,我可以通过终端运行我的程序。如果我通过终端打开IDE(例如Eclipse)然后运行应用程序,该程序也会运行。显然,.bash_profile
可以为终端应用程序设置变量。 如何设置窗口化应用程序的路径?
PS:此路径设置问题不仅仅适用于Boost,但我也必须为其他库做类似的事情(例如构建/安装在本地目录中的OpenCV)。 / p>
答案 0 :(得分:1)
您应该将库的rpath添加到依赖于boost的应用程序,而不是像这样添加系统范围的路径。要添加rpath
选项,请执行Project Properties
- &gt; C/C++ Build
- &gt; Settings
- &gt; Miscellaneous
并在链接器标志中添加:
-Wl,-rpath,/Users/myName/Documents/Softwares/boost_1_59_0/stage/lib
(例如,如果您的链接器为g++
或clang++
)
如果您的链接器是ld
显式的,那么选项是
-rpath /Users/myName/Documents/Softwares/boost_1_59_0/stage/lib
虽然您可能需要添加-macos_version_min 10.5
(或者更新 - 可能是10.8 - 这取决于您正在构建的操作系统)。
这将导致构建的所有应用程序都在那里搜索库以及默认位置。
虽然这样的位置看起来有些不稳定
结果表明boost构建时没有将库名设置为在安装名称中包含@rpath
,这意味着即使在构建中设置-rpath
,也不会提及库在@rpath
中,它不会在运行时找到它们。解决方法是显式设置boost库的安装名称,以及它们对自己库的内部引用:
#!/bin/bash -p
for i in *.dylib; do
# set the rpath
install_name_tool -id @rpath/$i $i
for lib in $(otool -L $i | grep libboost | awk '{print $1}'); do
if [[ -f $lib ]]; then
install_name_tool -change $lib @rpath/$lib $i
fi
done
done
这意味着链接到这些boost库的二进制文件将遵循rpath设置。
您可以为其他库重复类似的过程,以确保它们尊重rpath。关键元素是install_name_tool -id "@rpath/libstuff.dylib" libstuff.dylib
,它表示当您链接到库时记录对@rpath/libstuff.dylib
的引用。如果库还没有这个设置。
其次,对于依赖库的内部引用,-change
选项将对绝对名称的引用改为rpath相对名称,例如: install_name_tool -change "libstuff.dylib" "@rpath/libstuff.dylib" libdependsonlibstuff.dylib
。这甚至可以在链接的二进制文件上执行。
如果您仍想设置一个环境变量there are some options available,它可以帮助您获得适用于该情况的解决方案。