Dlopen issue (OSX)

I have a main application that dynamically loads dylib , from inside that dylib I would like to call exported functions from my main program. I use dlopen(NULL,flag) to retrieve the main applications handle and dlsym(handle, symbol) to get the function .

dlopen gives no error , but when I try dlsym my function , I get the following error :

 dlerror dlsym(RTLD_NEXT, CallMe): symbol not found 

The symbol is exported with a correction confirmed by nm. I'm not sure why RTLD_NEXT is RTLD_NEXT ? Is this the result of dlopen(NULL,flag) ?

How can I solve this problem or achieve my goal?

Or are there other ways to invoke the main application (preferably without passing function pointers to 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 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"); 
+4
source share
1 answer

I think the best approach might be to create a proprietary protocol with your dynamic library, where you initialize it by passing it the structure of function pointers. A dynamic library should simply provide some init(const struct *myfuncs) function init(const struct *myfuncs) or some such function, which simplifies the implementation of the dynamic library.

It will also make the transfer more realistic.

+4
source

Source: https://habr.com/ru/post/1381288/


All Articles