Delayed (dynamic) loading of a framework (or dylib) on Mac

I am trying to download a framework (or dylib) on a mac where I know the path only at runtime.

Windows Solution:

  • Link Library with / DELAYLOAD
  • Before using the function from the library, change the current directory in the program to the directory where the dll is located
  • Call some functions from this library. DLL is loaded from the current directory

On mac, I can use loose coupling to make the application launch without an accessible library. However, as soon as some functions from the library are needed, I get an "image not found", and the application is interrupted.

How can I tell the Mac dynamic linker at runtime where to look for the library? "dlopen" does not work because it only loads the library and does not allow characters. Setting rpath to "." (current directory) and changing the current directory does not work. Setting typical environment variables (DYLD_LIBRARY_PATH) only works when executed before the executable is run, and not at run time.

Any other ideas?

+4
source share
2 answers

Write a shell script or executable that detects the path at runtime, adds the path to DYLD_LIBRARY_PATH, and then calls execve on the real executable.

0
source

Use dlopen () to open the library, and then dlsym () to find the characters. If you rely on a dynamic linker, you must know the path in advance and set it either through rpath or through environment variables. Rpath can take relative paths, so this may work for you ... but most likely it is the best solution for dlopen () and friends (although it may take some work to convert to function pointers).

The advantage of dlopen (), etc. - This is the same (or similar) code that will work with other * nix.

You can also look at NSAddImage (), which is OSX specific, but should also do what you want.

-1
source

All Articles