创建一个可以弱链接的静态库

问题描述:

是否可以将对象/类添加到静态库中,以便在库弱链接时将它们排除?我尝试向我的obj c类添加属性,将它们标记为weak_import,但编译器说它未定义。

Is it was possible to add objects/classes to a static library in a way that would let them be excluded when the library is weak linked? I tried adding attributes to my obj c classes that tag them as "weak_import" but the compiler says it is undefined.

是, 有可能的。不幸的是,虽然运行时和链接器支持它,但编译器不支持,这意味着您需要为头中的类声明程序集存根。特别是,如果你想让MyClass变弱,你可以在 MyClass.h中执行此操作

Yes, it is possible. Unfortunately, while the runtime and linker support it, the compiler does not, which means you need to declare the assembly stubs for the classes in the headers. In particular, if you wanted to make MyClass weak you would do this in MyClass.h:

asm(".weak_reference _OBJC_CLASS_$_MyClass");
asm(".weak_reference _OBJC_METACLASS_$_MyClass");

@interface MyClass

@end

这仅适用于iOS 3.1及更高版本。有关详细信息,请参阅博客文章。

This will only work on iOS 3.1 and later. For more details read this blog post.