Troubles one might face while using dynamic libraries-
Coming from windows background, I thought using dynamic libraries in Xcode would be just as easy as copy dylibs files to project souce dir.. but no.. Xcode has some different ideas.
So, you added all the include folders, and you added the library paths in the project build settings.
You compile the program… NO ERRORS… you build it.. you see that BUILD WAS SUCCEEDED,
However, when you run it, you see something like this..
I hope you’re familiar with the things you see in the image above mean, whenever you build your project in Xcode, it create an app executable in the temporary folder for Xcode. This app file even though an executable with .app extension is just a directory, you can right click on it, and say Show Package Contents, and you can browse files inside your app. If you copy it on windows, you will simply find a folder named.. explicit_gfx.app. The main exec file is inside this directory, .app file simply encapsulates all the frameworks, and other resources.
Anyhow coming back to the issue in hand, my program is looking for ./libfmodex.dylib but its not finding it in the executable app file. But when I go inside the app file, I see it there nicely sitting in _.app/Contents/Frameworks/ directory, what went wrong?
You might say file is there but the path is incorrect.
No, actually the thing is, the name, the id of the file needs to be changed in order to use it in your executable.
This is a real pain in the ___ if you’re new in Xcode.
Its was this error because of which I had to include a static library of some framework.
But i eventually got to know the source of the problem.
If you code regularly in Xcode environment or in OS X for that matter, you might have heard of utility called install_name_tool.
It is used to change the properties of the dylib file.
So in order to fix the issue in hand, you will have to use this command on your dylib.
How to do that? —
- Open up terminal
- install_name_tool -id @rpath/libfmodex.dylib /[path_of_dylib]/libfmodex.dylib
- install_name_tool -change libfmodex.dylib @rpath/libfmodex.dylib /[path_of_dylib]/libfmodex.dylib
This did trick for me, [-id] command changes the name to the name you supplied @path…. in this case, and [-change] changes path of one reference library.
You can check man pages for install_name_tool for further information.