[英]Avoid symbol reference in static library
I develop a static library that is distributed to other developers.我开发了一个分发给其他开发人员的静态库。 I want to use CocoaLumberjack (DDLog) class if it is available in the final binary.
如果在最终二进制文件中可用,我想使用 CocoaLumberjack (DDLog) 类。 In the static library I define the class interface and check
[DDLog class]
to see if it exists.在静态库中我定义了类接口并检查
[DDLog class]
以查看它是否存在。 But in the host App, if CocoaLumberjack isn't present, the linker complains because DDLog does not exist.但是在宿主应用程序中,如果 CocoaLumberjack 不存在,链接器就会抱怨,因为 DDLog 不存在。
I know I can defer symbol checking to runtime in the App configuration, but is there a way to prevent the static library compilation from referencing the DDLog class in the compiled objects?我知道我可以在 App 配置中将符号检查推迟到运行时,但是有没有办法防止静态库编译引用编译对象中的 DDLog 类?
I don't think it is possible to have undefined symbols at final App link time, even if they are weak.我认为在最终的 App 链接时不可能有未定义的符号,即使它们很弱。 From the docs for Mac OS X ld:
来自 Mac OS X ld 的文档:
When creating a output file with the static link editor when -twolevel_namespace is in effect (now the default) all undefined references must be satisfied at static link time.
当 -twolevel_namespace 生效(现在是默认值)时,使用静态链接编辑器创建输出文件时,必须在静态链接时满足所有未定义的引用。 The flags to allow undefined references, -Usymbol_name, -undefined warning and -undefined sup_press can't be used.
不能使用允许未定义引用、-Usymbol_name、-undefined 警告和-undefined sup_press 的标志。 When the environment variable MACOSX_DEPLOYMENT_TARGET is set to 10.3 then -undefined dynamic_lookup can also be used.
当环境变量 MACOSX_DEPLOYMENT_TARGET 设置为 10.3 时,也可以使用 -undefined dynamic_lookup。
So if there are any undefined references at link time (including any API you use that is not in the current Base SDK version) it will produce an error.因此,如果在链接时有任何未定义的引用(包括您使用的任何不在当前 Base SDK 版本中的 API),它将产生错误。 The only way around this is to use the
-undefined dynamic_lookup
linker option.解决此问题的唯一方法是使用
-undefined dynamic_lookup
链接器选项。 Unfortunately this defers all symbol lookup to runtime, you can't specify just the symbols you want to skip when using two level namespace.不幸的是,这将所有符号查找推迟到运行时,在使用两级命名空间时,您不能仅指定要跳过的符号。
For me, I don't want to burden the end developer with that.对我来说,我不想给最终开发人员带来负担。 So I switched to using
objc_msgSend
and NSClassFromString
instead, avoiding all use of the symbol.所以我改用
objc_msgSend
和NSClassFromString
,避免使用所有符号。 It's a shame it has to be done in that way and this seems like something Apple could improve.很遗憾必须以这种方式完成,这似乎是 Apple 可以改进的地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.