dylib中自动执行构造函数代码
据我所知,当使用 dlopen 加载库时,构造函数中的代码会自动执行。
我如何为 iOS 框架执行此操作?我希望 dylib 在加载时在构造函数中运行一些代码。
在Apple的文档中(https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/CreatingDynamicLibraries.html),我看到了这个片段
// Initializer.
__attribute__((constructor))
static void initializer(void) { // 2
printf("[%s] initializer()\n", __FILE__);
}
// Finalizer.
__attribute__((destructor))
static void finalizer(void) { // 3
printf("[%s] finalizer()\n", __FILE__);
}
我尝试了以下
- 我创建的框架的 Xcode 项目。在框架中,我添加了上面的代码片段。然后我构建了框架的调试版本。
我想要做的是
- 修改用户的位码文件(从启用位码的 IPA 中提取)以将我自己的一些代码合并到其中。我的代码(用 C 编写并转换为位码以进行合并)将在 dylib 上调用 dlopen,当加载它时,构造函数代码将执行。
From what I read, code in the constructor is executed automatically when the library is loaded using dlopen.
How can I do this for a Framework for iOS? I want the dylib to run some code in the constructor when it is loaded.
In Apple's documentation (https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/CreatingDynamicLibraries.html), I saw this snippet
// Initializer.
__attribute__((constructor))
static void initializer(void) { // 2
printf("[%s] initializer()\n", __FILE__);
}
// Finalizer.
__attribute__((destructor))
static void finalizer(void) { // 3
printf("[%s] finalizer()\n", __FILE__);
}
I have tried the following
- I created an Xcode Project for a Framework. In the Framework, I added the above code snippet. Then I build a debug version of the Framework.
What I want to do is
- Modify a user's bitcode file (extracted from a bitcode enabled IPA) to merge some of my own code into it. My code (written in C and converted to bitcode for merging) would call dlopen on the dylib and when it is loaded, the constructor code will execute.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论