Question:
Is it possible to loosely link a static library (Obj-C)?
Brief information
I want my custom static structure ( MyFramework.framework ) to loosely link my other static library ( libMyLibrary.a ).
The functionality of libMyLibrary.a is optional and may be omitted if NO libMyLibrary.a is associated with any third-party application that uses MyFramework. framework .
I am using -weak_library . In my test application, it complains that the static linker cannot find the MyLibrary MyClass character inside the MyFramework ABCTracker.o :
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 set up weak communication?
Details
Customization
- The Xcode project creates a static Mach-O bitmap file and merges it into a static structure. The result is MyFramework.framework .
- Another project creates a static Mach-O bitmap file, and the result is a libMyLibrary.a static lib file with the title MyLib.h
- libMyLibrary.a is removed from MyFramework.framework of the target assembly phases> Link to binary files with libraries ( as suggested here ). Only MyLib.h is available to use the library API from infrastructure classes
- NO Bitcode is not used in the framework or in the library
- MyFramework.framework , libMyLibrary.a and the user application are written in Objective-C
- MyLib.h defines only one Objective-C class
MyClass - MyFramework.framework uses
MyClass from its own ABCTracker class, conditionally checking for a character at runtime, for example. NSClassFromString(@"MyClass") == NULL From MyFramework build target settings, I set Other Librarian Flags and Other Linker Flags for the same -weak_library MyLibrary :
OTHER_LDFLAGS = ( "-weak_library", MyLibrary, ); OTHER_LIBTOOLFLAGS = "-weak_library MyLibrary";
Result
- MyFramework.framework builds OK
After the assembly, I checked the characters in the resulting binary, and the output was emty (no characters from the static library were embedded in the static binary):
$ otool -L MyFramework.framework/MyFramework | grep MyClass
Despite this, my test application, which is not related to MyLibrary , is building with an 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?
Other observations
In the target MyFramework, I set Other Librarian Flags and Other Linker Flags the same value:
-lMyLibrary . Result: otool shows that library symbols are embedded in the structure (expected).-weak-lMyLibrary . The result is the same as for lMyLibrary (expected?)
In my application, I set Other Linker Flags to -force_load MyLibrary . Result: the linker error changes slightly:
ld: file not found: MyClass clang: error: linker command failed with exit code 1 (use -v to see invocation)
ios objective-c linker xcode static-libraries
Yevhen Dubinin Nov 17 '17 at 8:01 2017-11-17 08:01
source share