Is it possible to weakly link a static library (Obj-C)?
I do want my custom static framework (MyFramework.framework) to weakly link my other custom static library (libMyLibrary.a).
The functionality behind libMyLibrary.a is optional and can be omitted if there is NO libMyLibrary.a being linked by any 3rd party application that uses MyFramework.framework.
I am using -weak_library
. My test application complains that static linker is unable to find MyLibrary's symbol MyClass
within MyFramework's ABCTracker.o
symbol:
Undefined symbols for architecture arm64:
"_OBJC_CLASS_$_MyClass", referenced from:
objc-class-ref in MyFramework(ABCTracker.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
How to correctly setup weak linking?
MyClass
MyClass
from its own class ABCTracker
conditionally checking for symbol availability during runtime, e.g. NSClassFromString(@"MyClass") == NULL
From MyFramework target's Build Settings I have set Other Librarian Flags
and Other Linker Flags
to same value -weak_library MyLibrary
:
OTHER_LDFLAGS = (
"-weak_library",
MyLibrary,
);
OTHER_LIBTOOLFLAGS = "-weak_library MyLibrary";
After the build I have checked the symbols in the resulting binary and the output was emty (no symbols from the static library were built into static framework binary):
$ otool -L MyFramework.framework/MyFramework | grep MyClass
Despite that, my test application which is not linked with MyLibrary whatsoever, builds with ld
error:
Undefined symbols for architecture arm64:
"_OBJC_CLASS_$_MyClass", referenced from:
objc-class-ref in MyFramework(ABCTracker.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
What am I doing wrong here?
In MyFramework target I set Other Librarian Flags
and Other Linker Flags
of to same value:
-lMyLibrary
. Result: otool
shows the library's symbols are built into the framework (expected).-weak-lMyLibrary
. Result is the same as for lMyLibrary
(is it expected?)In my application target I set Other Linker Flags
to -force_load MyLibrary
. Result: the linker error slightly changes:
ld: file not found: MyClass
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have also not been successful at getting XCode to properly weak link a static library, although I have the opposite problem from yours - for me nm
showed all the symbols from the static library, and not with "U" (undefined) symbol type as you see when you weak link a framework.
But a workaround you can use is the following:
-ObjC
to the linker flags to make sure all the symbols get
loaded (and -all_load
if you need non Obj-C symbols)-weak_framework
to Other Linker Flags)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With