简体   繁体   English

避免静态库中的符号引用

[英]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_msgSendNSClassFromString ,避免使用所有符号。 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.

相关问题 iOS静态库到静态库符号未定义 - iOS Static Library into Static Library symbol not defined 与Worklight静态库的符号冲突 - Symbol collisions with Worklight static library 静态/动态库中的运行时 ObjC 符号冲突 - Runtime ObjC symbol collision in static/dynamic library 如何在iOS中共享静态库并避免重复? - How to share static library and avoid repetition in iOS? xCode 4.3静态库跨项目参考 - xCode 4.3 static library cross project reference 静态库需要参考项目配置文件 - Static Library needs to reference project configuration file iOS 4.3.5上静态库中未找到符号错误 - Symbol not found error from static library on iOS 4.3.5 静态库(ARC)在非ARC应用程序上工作,遇到错误:dyld:惰性符号绑定失败:未找到符号:_objc_retainAutoreleasedReturnValue - Static library(ARC) work on non ARC app, met error: dyld: lazy symbol binding failed: Symbol not found: _objc_retainAutoreleasedReturnValue 符号定义存在时“未定义的体系结构符号”clang 错误(使用静态库) - “Undefined symbols for architecture” clang error (using static library) while the symbol definition is present iPhone静态库Clang / LLVM错误:non_lazy_symbol_pointers - iPhone static library Clang/LLVM error: non_lazy_symbol_pointers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM