Reputation: 1141
In Linux Ubuntu I can compile C++ source code with dynamic library and tell the gcc compiler where to find the .so lib file by setting environment variable $LD_RUN_PATH before compile. I would like to know is there a equivalent method for clang compiler in OSX to do such run time dynamic library path search?
Thank you very much!
Edit:
The duplicate link given below is not what I want!
I would like to compile the search path for dylib into the executable. At runtime there should not be any path info set explicitly for $DYLD_LIBRARY_PATH environment path. Which is to say when you echo $DYLD_LIBRARY_PATH no path point to the dylib should be seen.
I achieved this by using
otool -L <executable>
to read the search path in my executable.
For any wrong or lack of search path, I use install_name_too to update or add the search path information.
For me I am changing my original linking path. I use
install_name_tool -change <old_path> <new_path> <executable>
for link path setting. After this I am able to run the executable correctly link to the dylib without setting anything inside $LD_LIBRARY_PATH or $LD_FALLBACK_LIBRARY_PATH.
Upvotes: 6
Views: 6801
Reputation: 127
On macOS with the LLVM compiler "clang" you have the -F{dir} and -I{dir} preprocessor options to search for Framework/Library directories and Include directories. These will the compiler where where to look for non-standard location where the *.dylib dynamic library files reside that are to be linked.
-Idirectory Add the specified directory to the search path for include files. -Fdirectory Add the specified directory to the search path for framework include files.
Regarding search behavior, these correspond in Xcode to the Project or Target "Build Settings" called Framework ~, Header ~ and Library Search Paths.
Whether you would want some dynamic library to get linked to your application is governed by the "General" setting "Linked Frameworks and Libraries".
More about how to manage the dynamic loader (dyld) and control the location of dependent libraries with @rpath and @loader_path macros is documented at: https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/RunpathDependentLibraries.html
Upvotes: 2