[英]Weakly link static library via -weak_library
Is it possible to weakly link a static library (Obj-C)? 是否可以弱链接静态库(Obj-C)?
I do want my custom static framework ( MyFramework.framework ) to weakly link my other custom static library ( libMyLibrary.a ). 我确实希望我的自定义静态框架( MyFramework.framework )弱链接我的其他自定义静态库( 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 . 背后libMyLibrary.a的功能是可选的,如果没有 libMyLibrary.a被使用MyFramework.framework任何第三方应用程序链接可以省略。
I am using -weak_library
. 我正在使用
-weak_library
。 My test application complains that static linker is unable to find MyLibrary 's symbol MyClass
within MyFramework 's ABCTracker.o
symbol: 我的测试应用程序抱怨静态链接器无法在MyFramework的
ABCTracker.o
符号中找到MyLibrary的符号MyClass
:
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
MyClass
from its own class ABCTracker
conditionally checking for symbol availability during runtime, eg NSClassFromString(@"MyClass") == NULL
ABCTracker
MyClass
有条件地在运行时检查符号的可用性,例如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
: 从MyFramework目标的Build Settings中,我将
Other Librarian Flags
和Other Linker Flags
为相同的值-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): 构建之后,我检查了生成的二进制文件中的符号,输出为emty(静态库中没有任何符号内置到静态框架二进制文件中):
$ otool -L MyFramework.framework/MyFramework | grep MyClass
Despite that, my test application which is not linked with MyLibrary whatsoever, builds with ld
error: 尽管如此,我的未与MyLibrary链接的测试应用程序仍会生成
ld
错误:
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: 在MyFramework目标中,我将
Other Librarian Flags
和Other Linker Flags
设置为相同的值:
-lMyLibrary
. -lMyLibrary
。 Result: otool
shows the library's symbols are built into the framework (expected). otool
显示库的符号已内置到框架中(预期)。 -weak-lMyLibrary
. -weak-lMyLibrary
。 Result is the same as for lMyLibrary
(is it expected?) lMyLibrary
相同(是预期的吗?) In my application target I set Other Linker Flags
to -force_load MyLibrary
. 在我的应用程序目标中,我将
Other Linker Flags
设置为-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. 我也没有成功地使XCode正确弱链接静态库,尽管我遇到了与您相反的问题-对我来说,
nm
显示了静态库中的所有符号,而不是“ U”(未定义)符号类型当您弱连接框架时,您会看到。
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) -ObjC
到链接器标志中,以确保所有符号-all_load
加载(如果需要非Obj-C符号,则添加-all_load
) -weak_framework
to Other Linker Flags ) -weak_framework
将其添加到Other Linker Flags )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.