dlsym casting problem?

Hi guys,
I am new to C++ and I have this casting problem with dlsym() (I use Debian Lenny)

In the following code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Snake.h"
#include <dlfcn.h>

int main(){

void *handle;
handle=dlopen("./libSnake.so.1",RTLD_LAZY);

if(!handle)
{
  fputs(dlerror(),stderr);
  exit(1);
}
ISnakeAlgo (*mkrfunc)();
void *mkr=dlsym(handle,"getInstance");
ISnakeAlgo *mysnake=static_cast<ISnakeAlgo*()>(mkr)();
.
.
dlclose(handle);
return 0;
}


Compiler gives the error "app.cpp:20: error: invalid static_cast from type ‘void*’ to type ‘ISnakeAlgo* ()()’"

My ISnakeAlgo interface is as follows :

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <stdlib.h>

class ISnakeAlgo {

public:
 virtual ~ISnakeAlgo(){};
 virtual int method()=0;
 virtual ISnakeAlgo* getInstance();
 virtual void start();
};


How to fix this compiler problem ?

Thanks
You need a reinterpret_cast there instead I believe.
Whoa... ok, firedraco's correct, but it's never going to work.

I'm assuming you are trying to refer to ISnakeAlgo::getInstance() when you are looking up the symbol.
But this function name has been mangled by the compiler, so I can guarantee that dlsym() will return
NULL.

You are going to need to make a free function declared with extern "C" in order for this to work.

1
2
3
4
// In one of your .so header files:
extern "C" {
    ISnakeAlgo* getInstance();
};


And now you'll be able to look up "getInstance" with dlsym.
Topic archived. No new replies allowed.