1

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?

Alexis King
  • 43,109
  • 15
  • 131
  • 205
  • Why not `dlopen()` the executable and `dlsym()` the function? I don't think that the current approach is feasible. –  Feb 03 '13 at 10:01
  • @H2CO3 I'm already `dlopen`ing the library I'm loading. My goal is to then be able to call a function that resides in the executable loading the library. – Alexis King Feb 03 '13 at 10:02
  • Read my comment again. "Why not `dlopen()` **the executable**?" –  Feb 03 '13 at 10:03
  • @H2CO3 I *did* read that, but [this](http://stackoverflow.com/a/14348800/465378) answer seems to indicate that I shouldn't have to do that. Even if I do need to do that, how would I determine the location of the executable loading the library? – Alexis King Feb 03 '13 at 19:40

1 Answers1

0

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=hidden flag 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.

Alexis King
  • 43,109
  • 15
  • 131
  • 205