防止 Xcode 从静态库中剥离未使用的符号 [英] Keeping Xcode from stripping out unused symbols from a static library

查看:45
本文介绍了防止 Xcode 从静态库中剥离未使用的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个静态库,我的 iOS 二进制文件正在链接该库.静态库的代码剥离已关闭,等等,我可以看到存档中的符号(通过 nm).

I've built a static library that I'm my iOS binary is linking in. Code stripping is off, etc. for the static library, and I can see the symbols in the archive (via nm).

因此,我将该库作为框架链接到我的应用程序中.但是,我实际上并没有直接调用该函数.因此,我可以在我的二进制文件中看到我直接调用的符号,但看不到未被调用的符号.但是,因为我实际上希望这些符号在运行时就在那里,所以我使用 -all_load 进行编译.

So, I link that library into my application as a framework. But, I'm not actually calling that function directly. As a result, I can see the symbols that I'm calling directly in my binary, but not the ones that aren't called. But, since I actually want these symbols to be there at runtime, I'm compiling with -all_load.

但是,这似乎给图书馆的用户带来了不必要的负担.是否可以在静态库中添加一些强制执行此操作的内容,而不是依赖库的用户?

But, this seems to place an unnecessary burden on the users of the library. Is there something I can add in the static library that enforces this, rather than relying on the user of the library?

推荐答案

根据您要完成的任务,您 可以精确控制哪些符号被删除并且始终加载,即使库的用户实际上并不使用它们.

Depending on what you are trying to accomplish, you can precisely control which symbols are dead-stripped and which are always loaded, even if the user of the library doesn't actually use them.

最简单的方法是创建一个库初始化函数,该函数引用您不想完全剥离的确切符号.这是精确的,并为您节省了与链接器命令行选项搏斗的负担,这可能会使您免受(不太可能的)工具行为更改的影响.

The easiest way is to create a library initialization function that references the exact symbols you don't want dead-stripped. This is precise, and saves you the burden of wrestling with the linker command-line options, which may insulate you from (unlikely) tool behavior changes down the road.

框架具有自动初始化程序(非常方便),可以在运行时加载框架时自动调用,在初始化任何静态变量后立即调用.

Frameworks have automatic initializers (quite handy) that can be called automatically when the framework is loaded at runtime, right after any static variables are initialized.

__attribute__((constructor))
static void MyModuleInitializer()
{
    static BOOL initialized = NO;
    if (!initialized) {

        // References to symbols that should be kept around.

        initialized = YES;
    }
}

只是为了咧嘴笑,使用 __attribute__((destructor)) 装饰器也支持自动终结器.

Just for grins, automatic finalizers are also supported using the __attribute__((destructor)) decorator.

这篇关于防止 Xcode 从静态库中剥离未使用的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆