dlopen 问题(OSX)
我有一个动态加载 dylib 的主应用程序,从该 dylib 内部我想从主程序调用导出的函数。我正在使用 dlopen(NULL,flag) 来检索我的主要应用程序 handle
和 dlsym(handle, symbol)
来获取函数
。
dlopen
给出没有错误
,但是当我尝试dlsym
我的function
时,我收到以下错误
code>:
dlerror dlsym(RTLD_NEXT, CallMe): symbol not found
符号已导出并已通过 nm 我不确定为什么会有 RTLD_NEXT
?这是 dlopen(NULL,flag) 的结果吗?
我该如何解决这个问题或实现我的目标?
或者是否有其他方法来调用主应用程序(最好不要将函数指针传递给 dylib)?
提前致谢!
添加:
导出:
extern "C" {
void CallMe(char* test);
}
__attribute__((visibility("default")))
void CallMe(char* test)
{
NSLog(@"CallMe with: %s",test);
}
dylib中nm代码的结果
...
0000000000001922 T _CallMe
..
:
void * m_Handle;
typedef void CallMe(char* test);
CallMe* m_Function;
m_Handle = dlopen(NULL,RTLD_LAZY); //Also tried RTLD_NOW|RTLD_GLOBAL
if(!m_Handle)
return EC_ERROR;
m_Function = (CallMe*)dlsym(m_Handle, "CallMe");
if(!m_Function)
return EC_ERROR;
m_Function("Hallo");
I have a main application which dynamically loads a dylib
, from inside that dylib
I would like to call exported functions from my main program. I'm using dlopen(NULL,flag)
to retrieve my main applications handle
and dlsym(handle, symbol)
to get the function
.
dlopen
gives no error
but when I try to dlsym
my function
I get the following error
:
dlerror dlsym(RTLD_NEXT, CallMe): symbol not found
The symbol is exported corrected confirmed by nm
I'm not sure why RTLD_NEXT
is there? is this the result of dlopen(NULL,flag)
?
How can I solve this problem or achieve my goal?
Or are there other ways to call the main application (preferably not by passing on function pointers to the dylib)?
Thanks in advance!
Added:
Export:
extern "C" {
void CallMe(char* test);
}
__attribute__((visibility("default")))
void CallMe(char* test)
{
NSLog(@"CallMe with: %s",test);
}
Result of nm
...
0000000000001922 T _CallMe
..
Code in dylib:
void * m_Handle;
typedef void CallMe(char* test);
CallMe* m_Function;
m_Handle = dlopen(NULL,RTLD_LAZY); //Also tried RTLD_NOW|RTLD_GLOBAL
if(!m_Handle)
return EC_ERROR;
m_Function = (CallMe*)dlsym(m_Handle, "CallMe");
if(!m_Function)
return EC_ERROR;
m_Function("Hallo");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为更好的方法可能是使用动态库建立专有协议,通过向其传递函数指针结构来初始化它。动态库只需要提供某种 init(const struct *myfuncs) 或类似的函数,这使得动态库的实现更加简单。
这也将使实现更加可移植。
I think a better approach might be to establish a proprietary protocol with your dynamic library where you initialise it by passing it a struct of function pointers. The dynamic library needs to simply provide some sort of
init(const struct *myfuncs)
, or some such, function and this makes it simpler to implement the dynamic library.This would also make the implementation more portable.