Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dlopen issue(OSX)

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");
like image 434
Johan_ Avatar asked Nov 15 '11 12:11

Johan_


1 Answers

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.

like image 128
trojanfoe Avatar answered Sep 21 '22 14:09

trojanfoe