OSX runtime dynamic library path search
Asked Answered
M

1

6

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.

Misfit answered 24/4, 2016 at 5:55 Comment(5)
See here: #3146774Jitters
@JeremyFriesner I have looked into this link and tried myself by setting both DYLD_LIBRARY_PATH and DYLD_FALLBACK_LIBRARY_PATH. This only solve the dynamic library path search at compile time. When executing the compiled file at runtime there is still error on finding the lib path. This is not what I am looking for.Misfit
When executing the compiled file at runtime there is still error on finding the lib path. - Looks like your usage of DYLD_LIBRARY_PATH is wrong. Note, that variable should be set when you run your executable.Jerri
@Jerri Well my question is not about how to set DYLD_LIBRARY_PATH. It is obvious that exporting DYLD_LIBRARY_PATH permanently to .bash_profile or .bashrc file can solve this problem at run time. But what I want is to compile the search path directly into executable. This can be achieved at compile stage by setting LD_RUN_PATH in Ubuntu Linux. I would like to know if there is an equivalent environment variable for OSX. From searching the OSX document I found there is no such environment variable. However there is a way to achieve this. I will add to OPMisfit
Oh, your previous comment was confusing, so it looked like you search for DYLD_LIBRARY_PATH variable. Actually, even with original wording your question is clearly not a duplicate. Sorry for inconvenience. Voted for reopen.Jerri
E
2

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

Elmoelmore answered 16/12, 2020 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.