5

A C++ dependent library .dylib resides in a bundle located in the app package's Content/Frameworks. I'd like to DELAY the loading of that dependent library until I've completed some specific initialization.

Is there any way OTHER THAN to create a Runtime-Loaded Library? Would using weak linking options prevent the .dylib from being loaded until first referenced?

SMGreenfield
  • 1,680
  • 19
  • 35

1 Answers1

3

You mean lazy linking:

ld -o test test.o -lazy_library /usr/lib/libz.dylib
ld -o test test.o -lazy-lz

Both load the Zlib compression library when a routine from it is first run. The problem is not to run the routines from your custom library before the initialization is finished.

Weak linking means "if library is missing, set all its symbols to NULLs, don't halt".

NOTICE: replace Zlib (/usr/lib/libz.dylib) with your library.

Top Sekret
  • 748
  • 5
  • 21
  • Very cool trick! I did get this error: ld: illegal data reference to __ZN9WBRefSpecD1Ev in lazy loaded dylib Where the mangled symbol refers to the class destructor: _WBRefSpec::~WBRefSpec() What causes that? – SMGreenfield May 12 '16 at 21:36
  • IDK. Maybe some symbols can **NOT** be weak? – Top Sekret May 13 '16 at 19:40
  • As you pointed out, it's not about WEAK, it's about LAZY. Either way -- NO ONE seems to know the answer to the illegal data reference error, so I'll have to ask Apple Developer Support... – SMGreenfield May 13 '16 at 23:22
  • NOTE: The answer to the illegal data reference was that there was a .cpp file with a class member function that declared "static WBRefSpec foo;" Removed that, and bingo, no link error. – SMGreenfield May 14 '16 at 00:07
  • Unfortunately these options have been removed: https://developer.apple.com/forums/thread/131252 – yugr Nov 25 '21 at 08:11