2

Recently I encountered this documentation about Apple and the loading of dynamic libraries.

Citing:

When the library name is a filename (that is, when it doesn’t include directory names), the dynamic loader searches for the library in several locations until it finds it, in the following order:

  1. $LD_LIBRARY_PATH
  2. $DYLD_LIBRARY_PATH
  3. The process’s working directory
  4. $DYLD_FALLBACK_LIBRARY_PATH

And my concern is about the third step "The process’s working directory".

Since I load libraries (and libraries that I load will load others), I would like to force the working directory to be some predefined path.

At the beginning of the main is already too late for me.

Do you know if there is some build option that I can set in pkgbuild, or in some .plist file?

Do you know a workaround for that?

EDIT:

I found these keys that could be set, in particular see LSEnvironment. I was thinking maybe some 'hack' like

$DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$DYLD_FALLBACK_LIBRARY_PATH

(yes, I know that it is ugly...). But it would not work anyway, because citing the previous link:

These environment variables are set only for apps launched through Launch Services. If you run your executable directly from the command line, these environment variables are not set.

So, what can I do if someone calls my executable directly?

Are the .plist files considered at all in case the executable is called directly?

Community
  • 1
  • 1
n3mo
  • 663
  • 8
  • 23
  • Check out https://stackoverflow.com/questions/9798623/how-to-properly-set-run-paths-search-paths-and-install-names. It could be helpful. – Mattie Mar 21 '19 at 15:33
  • Hi @Mattie. Thanks for the link, interesting. But I do not think it is my case. The dylib in my case are not built by me. I just use them, and they link to others dylib. – n3mo Mar 25 '19 at 09:19
  • That does make things more complex, but you can use `install_name_tool` to rewrite entires for existing dylibs. I’ve had to do this on occasion. It could still be a feasible solution... – Mattie Mar 25 '19 at 10:25

0 Answers0