5

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");
Community
  • 1
  • 1
Johan_
  • 440
  • 5
  • 14
  • Seems strange that you have `RTLD_NEXT` there. Can you post your actual code (including the `flag`s)? – Mat Nov 15 '11 at 12:28
  • Also post the output of nm and strings utility used on dylib. – RLT Nov 15 '11 at 12:42
  • This can help I hope: http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html Its where I have learned _everything i needed_ to use shared libs. I, too, have Mac OS. A program I wrote on linux worked, no idea why and please dont ask me, the first time i ran it, the second and rest times not. Check the file endings and internal file type, not just endings. Fetch some info about Mac `.bundle` files, and worry how weird their use is. Also fetch info about loading usual .so file in mac, which is a VERY unusual activity yo be done! My chars used up. Guud luck hope you do it better :D – imacake Nov 15 '11 at 13:00
  • How about trying just this in the dynamic library: `m_function = (CallMe *)dlsym(RTLD_DEFAULT, "CallMe");` ? – trojanfoe Nov 15 '11 at 13:27
  • Same result with RTLD_DEFAULT – Johan_ Nov 15 '11 at 13:32

1 Answers1

5

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.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • Why should this be a better approach? It will not make it better portable anyway, or am I *too* much linuxified? (Nevermind, I cant read) – imacake Nov 15 '11 at 13:07
  • It's a better approach as you are not making the dynamic library *search* for the functions itself; the main application presents them in an easy to use way. It's more portable as you don't have to use system calls to do the searching; you are using simple C-language methods. – trojanfoe Nov 15 '11 at 13:09
  • This is an option but it's a cross-platform library and the windows version solves the problem with GetModuleHandle(NULL) in a OS specific class so my initial plan was to implement the same strategy in the OSX specific class. But if this problem won't be solved I'll definitely go for the function struct initialisation option. Thank you! – Johan_ Nov 15 '11 at 13:26
  • I actually use that in my own program (sorry @trojanfoe). There i can emulate multiple 'parent' applications (As i have a TREE of libraries loaded, a master one takes care of its pupils). Shortly: Its more **flexible**. Nice approach. But if now the init function wants to depend on some other lib and loads it, that lib depends on the thing just before, they'll load eachother again and again. Thats the programmers fault then. Very cool approach. (--> _Design by contract_) – imacake Nov 15 '11 at 14:04
  • 1
    It's not clear what you mean by *that approach*. Are you answering me or @Johan_ ? – trojanfoe Nov 15 '11 at 14:08
  • Yours, torjan. Im sorry. Does johan submit some approach there though? I said that I could not read above, too. – imacake Nov 15 '11 at 14:15