how to use compiled .so in C++

Linux specific forum
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

how to use compiled .so in C++

Post 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.. :)
pb 5.11
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

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

Post 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:
pb 5.11
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

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

Post 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();
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

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

Post 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..
pb 5.11
Post Reply