Page 1 of 1

how to use compiled .so in C++

Posted: Thu May 16, 2013 4:11 pm
by gnasen
Hello folks,

I'm pretty new to programming in c++ and ran into the following problem:
I want to etablish a network connection between a windows and a linux system and use the pb network lib for this task.
However if I try to compile a simple .so lib like this

Code: Select all

ProcedureCDLL.i go()
  MessageRequester("test","hello")
EndProcedure
and save it as libtest.so I dont find a solution to use it in my c++ code. I added it to the linker (Makefile) with "-ltest", but now I have no idea how to go on.
Normally Ive got some .H file to include...

Help.. :)

Re: how to use compiled .so in C++

Posted: Fri May 17, 2013 9:48 am
by gnasen
I got it working with this code in the main part:

Code: Select all

void *handle;
handle = dlopen ("mypath/mylib.so", RTLD_LAZY);
void (*go)();
*(void **)(&go) = dlsym(handle, "go");
go();
dlclose(handle);
However I dont understand why I need to call dlsym with this *(void **) prefix. I dont even understand what it should mean. dlsym should in my opinion return a pointer to the function, why can I not retrieve it with this:

Code: Select all

void (*go)();
go = dlsym(handle, "go");
This confuses me.. :shock:

Re: how to use compiled .so in C++

Posted: Fri May 17, 2013 3:24 pm
by Fred
You only defined a new type, try this (not tested but it should work):

Code: Select all

void (*go_type)();
go_type go;

go = dlsym(handle, "go");
go();

Re: how to use compiled .so in C++

Posted: Wed May 22, 2013 11:39 am
by gnasen
Thank you very much, it pointed me to the right direction, it works now like this:

Code: Select all

typedef int (*go_type)();
go_type go;
go = (go_type)dlsym(handle, "go");
go();
Still I dont get all that pointer mess in c++. I have still no idea how to read this *(void **)(&go). It makes c very hard to learn if such stuff is used in tutorials and not really explained. Im still looking for a good book (not expert level) where I could find an explenation for this kind of stuff..