I have a specialized shared library that is loaded dynamically during the execution of an executable. This library provides the function b(). This function, in turn, calls a function a().
The a function is defined in the executable, not the library itself, so I would like to be able to call some of my executable’s code from the library. I’ve managed to use the -undefined dynamic_lookup flag to make clang leave these symbols for lazy binding, but dyld still complains when I try to run the executable:
dyld: lazy symbol binding failed: Symbol not found: _a
Referenced from: /usr/local/lib/myLib.dylib
Expected in: flat namespace
How can I get this symbol linked?
As it turns out, this was caused by Xcode not exporting the symbol by default. You can fix this by changing the “Symbols Hidden By Default” option under the LLVM Code Generation build settings. If you’re not using Xcode, this controls whether or not to use the
-fvisiblity=hiddenflag on the command line.Alternatively, you can leave this option turned on and selectively export symbols by adding the
__attribute__((visibility("default")))attribute to the functions you want to export.