Hi!
I'm creating a static audio engine library (call it Library A) that's intended for public consumption, but I want to include within it support for some features of another, separate library I'm building ("Library B", which influences the audio engine) that may or may not be present in the final compiled application.
I'm informed via a flag within the audio engine library that Library B is doing something (when, of course, Library B is present within the compiled app), whereupon I want to call a Library B function.
These are two separate projects, and I don't want to have either one rely on the other, but it would be beneficial for them to work together if they're both present within the final app.
I don't really have much experience with the finer points of compiling/linking, but it sounds to me like a weak symbol is what I'm after.
So, I tried to declare the function from Library B within the source code for Library A as a weak external function:
#define kAFlagIndicatingSomethingHappenedInLibraryB (1<<12)
extern void LibraryBFunction() __attribute__((weak));
...
if ( (flags & kAFlagIndicatingSomethingHappenedInLibraryB) && LibraryBFunction != NULL ) {
LibraryBFunction();
}
This shows up as an undefined symbol in the compiled Library A, as expected:
$ nm libA.a
...
U _LibraryBFunction
The problem is if I now link libA.a with an app missing Library B, I still get undefined symbol errors, missing LibraryBFunction.
I'm vaguely aware that there's some sequence of compiler/linker flags I can add (like -undefined dynamic_lookup, etc) to suppress these problems, but I'd really rather not require the end developer to add these flags to their build (plus it sounds like a bad idea for production code).
I'm also vaguely aware that there may be an attribute I can supply (weak_import, maybe?) that will cause the flagged method to not actually be linked in to the final binary, allowing a function to be overwritten during the link. Is there perhaps a way I can use this to define a stand-in, stub LibraryBFunction within A that will allow an app without Library B to be compiled against Library A, without inducing undefined symbol errors, but still allow an app *with* Library B to be linked with Library A and have my Library A-Library B support functional?
Anything else I might have missed?
Many thanks in advance,
Michael