All:
I have successfully set up two projects (Hello and World) in one workspace (HelloWorld). These are simple projects that are supposed to work together as a main project and a shared library project. This is the structure:
HelloWorld
Hello
src
Hello.cpp
Debug
src
Hello - [x86_64/le]
World
src
World.cpp
World.h
Debug
src
libWorld.dylib - (x86_64/le]
I have followed all instructions and finally was able to get them to compile with no errors. However, when I try to run the Hello project I receive the following error:
dyld: Library not loaded: libWorld.dylib
Referenced from: /Users/pdl/Development/HelloWorld/Hello/Debug/Hello
Reason: image not found
I posted the code below. It is super simple and the problem (I believe) is somewhere in the Eclipse configuration.
Thank you in advance for your help.
-------------------------------------------- Source Code ----------------------------------
Hello.cpp
#include <stdio.h>
#include "World.h"
int main() {
printf("Hello %s\n", getWorld().c_str());
return 0;
}
World.cpp
#include "World.h"
std::string getWorld() { return "World"; }
World.h
#include <string>
std::string getWorld();
dyld: Library not loaded: libWorld.dylib
Referenced from: /Users/pdl/Development/HelloWorld/Hello/Debug/Hello
Reason: image not found
This means that your program is using a dynamic library called libWorld.dylib, although you linked your dynamic library to your program during compiling. But you have to tell your program where is the dylib in run-time.
There are two solutions:
Solution 1: set up dynamic library environment variables for your project in Eclipse
:
1.Right click your project name
, select Run As->Run Configuration
2.Under Environment
tab click New
3.Put DYLD_LIBRARY_PATH
or DYLD_FALLBACK_LIBRARY_PATH
in Name
box
4.Put the path where your libWorld.dylib
file is in Value
box
For example:
if libWorld.dylib
file is in /opt/local/lib/my_dylib
folder, than put the path in the Name
box
Solution 2: set DYLD_LIBRARY_PATH
in your bash configuration file
1.Normally in Mac OS, configuration file is .profile
under ~/
folder, if you don't have this file than create a new file with the same name
2.Edit that file:
Add this line in your .profile
file:
export DYLD_LIBRARY_PATH=PATH_TO_YOUR_DYLIB:$DYLD_LIBRARY_PATH
in my example the PATH_TO_YOUR_DYLIB is opt/local/lib/my_dylib
, so you just add:
export DYLD_LIBRARY_PATH=/opt/local/lib/my_dylib:$DYLD_LIBRARY_PATH
in .profile file
3.Problem solved, in this solution, you don't have to set up dylib path for all eclipse project
PS. DYLD_LIBRARY_PATH is an environment variable to specify the path of your dynamic library
Difference between DYLD_LIBRARY_PATH
and DYLD_FALLBACK_LIBRARY_PATH
please refer to this post