I have installed boost libraries. I am using Eclipse to make a simple boost project.
#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;
}
I have already set the path of include folder and library folder in the properties of this project. But I was getting a runtime error: dyld: Library not loaded: libboost_system.dylib
. To solve this problem, I went to run configuration
and set the environment variable DYLD_LIBRARY_PATH
to /Users/myName/Documents/Softwares/boost_1_59_0/stage/lib
. This has solved my problem.
What I need: I don't want to set the environment variable
for each Boost-based project. Therefore, I tried to set my .bash_profile
. I wrote the following lines in .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
PROBLEM: By setting the variable in .bash_profile
, I am able to run my program through the terminal. The program also runs if I open the IDE (eg Eclipse) via terminal and then run the application. Apparently, .bash_profile
can set the variable for terminal applications. How can I set the path for windowed applications too?
PS: This path setting problem is not just for Boost only, but I have to do similar things for other libraries too (such as OpenCV which is build/installed in a local directory).
Rather than adding a system wide path like this, you should add the rpath to the libraries to applications that depend on boost. To add the rpath
option, you do Project Properties
-> C/C++ Build
-> Settings
-> Miscellaneous
and in the linker flags add:
-Wl,-rpath,/Users/myName/Documents/Softwares/boost_1_59_0/stage/lib
(this is if your linker is g++
or clang++
, for example)
If your linker is ld
explicitly, then the option is
-rpath /Users/myName/Documents/Softwares/boost_1_59_0/stage/lib
although you may need to add -macos_version_min 10.5
(or newer - probably 10.8 - this depends on the OS you're building on).
This will cause any applications built to search there for libraries as well as the default locations.
Although a location like that to me looks a bit volatile
Turns out that boost builds without setting the library name to include @rpath
in the install name, which means that even when you set the -rpath
in the build, because the libraries aren't mentioned to be in the @rpath
, it won't find them at run time. A workaround to this is to explicitly set the install name for the boost libraries, and their internal references to their own libraries:
#!/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
This means the binaries that are linked to these boost libraries will respect the rpath setting.
You can repeat a similar process for other libraries to ensure that they respect rpath. The key element is the install_name_tool -id "@rpath/libstuff.dylib" libstuff.dylib
, which says that when you link to the library record a reference to @rpath/libstuff.dylib
. If libraries don't already have this set.
Secondly, for internal references to dependent libraries, the -change
option alters references to the absolute name to an rpath relative name eg install_name_tool -change "libstuff.dylib" "@rpath/libstuff.dylib" libdependsonlibstuff.dylib
. This can even be performed on a linked binary.
If you still want to set an environment variable, there are some options available which should help you getting a solution which works in that case.
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.