在Xcode中查找与项目链接的框架

5

我看到了这个问题:如何在ios项目中编程获取包含的框架和库的列表?,它试图回答类似的问题。然而,我对此有两个问题。

  1. The answer in above link (or see code below) - does it provide all frameworks or "only" frameworks that are linked to the project.

    for (NSBundle *framework in [NSBundle allFrameworks]) 
        NSLog(@"%@",framework.bundlePath.lastPathComponent);
    
  2. if I see a framework appearing in above code, how can I find its usage in my code. I see many frameworks being referred to in above code, but I'm not able to figure out where exactly are they used. As per my knowledge, few of them are not used - is there a proper way to find this out.

更新 1: 我创建了一个非常简单的新应用程序,其中没有任何代码。然后,我执行了上面的for循环,并发现它也向我展示了所有框架 - 这意味着上面的代码只是打印了所有框架,而不是我实际将在我的应用程序中使用的框架。但是,这是否意味着所有打印出来的框架都与应用程序链接呢?

2
我无法为你提供答案,但在过去,我们称之为“DLL地狱”。 - user7014451
你是否在寻找类似于 otool -L MyApp 的输出结果? - Mats
1个回答

2
动态装载器dyld(3)提供了这些信息。以下代码将打印所有已加载的框架和共享库:
#include <stdio.h>
#include <dlfcn.h>
#include <mach-o/dyld.h>

int main() {
    uint32_t c = _dyld_image_count();

    for(uint32_t i = 0; i < c; ++i) {
        printf("%d: %s\n", i, _dyld_get_image_name(i));
    }
    return 0;
}

编辑:allFrameworks仅列出与您的应用程序链接且包含至少一个Objective-C类的框架(请参见https://developer.apple.com/documentation/foundation/nsbundle/1408056-allframeworks)。

通常情况下,查找引用者应该非常困难。如果您只是寻找单个函数,则可以添加函数的静态实现并从中调用已加载的变体。例如,这种技术用于覆盖__cxa_throw,但它也适用于其他函数:

static cxa_throw_t sCxa_throw = 0;

extern "C" void __cxa_throw(void *inException, void *inPvtinfo, void (*inDestination)(void *)) {
    if (sCxa_throw == NULL) {
        sCxa_throw = (cxa_throw_t)dlsym(RTLD_NEXT, "__cxa_throw");
    }
    sCxa_throw(inException, inPvtinfo, inDestination);
}

变量 sCXA_throw 包含此函数的动态版本的引用,而加载器使用静态版本。在此函数内,您可以使用 libunwind 解开堆栈以确定调用者。

我在问题中添加了代码并提供了完全相同的信息,但是这里不包含后面在代码中引用的库。 - prabodhprakash
抱歉,我误解了你的问题。我已经扩展了我的回答。 - clemens

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接