在iOS中使用dlsym按名称调用函数 [英] Calling function by name using dlsym in iOS

查看:1073
本文介绍了在iOS中使用dlsym按名称调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能在iOS中按名称调用函数吗?我有一个名为 getstring 的C函数。我将其称为如下:

Can't I call a function by name in iOS? I have a C function called getstring. I am calling it as follows:

void* handle = dlopen(NULL, RTLD_NOW);
if (handle)
{
fp func = dlsym(handle, "getstring");
if (!func)
    responseField.text = [NSString stringWithUTF8String:dlerror()];
else {
    char* tmpStr = func();
    responseField.text = [NSString stringWithUTF8String:tmpStr];        
}
}
else {
responseField.text = [NSString stringWithUTF8String:dlerror()];
}

执行此操作时, responseFiled.text 设置为 dlsym(...):找不到符号。这意味着 dlopen 有效但不是 dlsym 。我使用 nm 将符号转储到二进制文件中,并且看到 _getstring 存在。我检查手册对于 dlsym ,它说我不应该在名称中添加下划线。无论如何,添加它并不能解决问题。我究竟做错了什么?

When this executes, responseFiled.text is set to dlsym(...): symbol not found. This means dlopen works but not dlsym. I dumped the symbols in the binary using nm and saw that _getstring is present. I checked the manual for dlsym and it says I should not add an underscore to the name. Adding it does not solve the issue anyway. What am I doing wrong?

我问了一个类似的问题 here 关于在Objective-C中按名称调用函数,然后在Mac上按照答案成功尝试,因此这个问题似乎特定于iOS。

I had asked a similar question here about calling functions by name in Objective-C and then tried it successfully on a Mac following the answers, so this problem seems to be specific to iOS.

推荐答案

我在类似你的案件中确实有过一些成功经验。我用 dlsym(RTLD_MAIN_ONLY,getstring)来获取函数指针。

I've actually had some successful experiences in a case similar to yours. I used dlsym(RTLD_MAIN_ONLY, "getstring") to get the function pointer.

注意你的 getstring 符号必须适合动态链接:可以使用

Note that your getstring symbol must be suitable for dynamic linking: this can be checked using

nm -m <application>

您的符号必须是外部(不是非外部)。

Your symbol must be external (not non-external).

我还不太清楚确保符号被标记为<的程序code>外部。

I'm not yet too sure about the procedure to ensure that symbols are marked as external.

这篇关于在iOS中使用dlsym按名称调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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