ios - dlopen 有没有办法在 iOS 上重新加载 dylib 文件?

标签 ios dylib dlopen

正如标题所说...我对 dlopen() 很感兴趣。我知道应用商店不允许这样做,但我对 iOS 上的这个感到好奇。

我遇到的问题是我可以创建一个 .dylib 文件,并且可以在运行时使用以下代码加载该文件

char *dylibPath = "/Applications/myapp.app/mydylib2.dylib";

void *libHandle = dlopen(dylibPath, RTLD_NOW);
if (libHandle != NULL) {
    NSString * (*someMethod)() = dlsym(libHandle, "someMethod");
    if (someMethod != NULL)  {
        NSLog(someMethod());
    }
    dlclose(libHandle);
}

这取自here .

我遇到的问题是,如果我更改 mydylib2.dylib,dlopen 不会加载重新编译的 .dylib 文件。相反,它解析函数 someMethod 的旧版本。

例如,如果 someMethod 首先返回 @"Hello" 并且我将其更改为 @"Hello World" 并重新编译,上面的代码将始终返回 @"Hello" 直到我在模拟器中重新启动应用程序。

有人知道为什么会这样吗?并提出解决方法,以便可以在运行时重新加载此 .dylib?

最佳答案

查看 man page对于 dlclose,似乎有几个原因导致库无法卸载。

There are a couple of cases in which a dynamic library will never be unloaded: 1) the main executable links against it, 2) An API that does not supoort unloading (e.g. NSAddImage()) was used to load it or some other dynamic library that depends on it, 3) the dynamic library is in dyld's shared cache.

我怀疑您的问题是“dyld 共享缓存”。你可以尝试运行:

sudo update_dyld_shared_cache -force

在您替换库之后(不确定这是否在 iOS 上可用)。

您是否检查过dlclose 的返回值以查看它是成功还是失败?

关于ios - dlopen 有没有办法在 iOS 上重新加载 dylib 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12653167/

相关文章:

ios - 从 UIPopoverController 显示时,不会调用 UITableView 的自定义委托(delegate)

ios - 视频从应用程序上传到 youtube

切换到 OpenCV 的自制程序后,Xcode 找不到 dylib

jailbreak - 编译 iPhone 调整时出错

linux - "same library"的 dlopen 定义

ios - Swift UserDefaults 可选 Int

ios - 以编程方式将 subview 添加到 Storyboard 中的预定义 subview 不起作用

ios - 使用LC_LOAD_DYLIB加载的动态库插入C函数

c++ - 以编程方式获取共享库中的函数名称

c - 使用 dlopen 访问 CMake 生成的动态库