Weakly link the static library via -weak_library

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) 
+7
ios objective-c linker xcode static-libraries
Nov 17 '17 at 8:01
source share

No one has answered this question yet.

See similar questions:

12
Linking static libraries sharing a static library

or similar:

71
build failed: ld: duplicate character _OBJC_CLASS _ $ _ Algebra5FirstViewController
65
file was created for an archive that is not related to architecture (i386)
fifty
static link variable error
one
Attempting to import GoogleMobileAds causes compilation (link)
0
IOS Xcode Static Library (Layout Errors)
0
troubleshooting link errors with ios static library workspace
0
Linker command error with exit1 code in iOS when using a static library file
0
Undefined characters for x86_64 architecture when using a static library
0
Xcode Framework wrapper for the library (publishing library headers)



All Articles