[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: dlopen -> no references from host



Felix Kollmann wrote:

> But now I have an other problem:
> 
> I try to import a class. I have to possibilities:
> 
> 1. putting the reference of the class into the host -> there is no
> symbol in
>    the client
> 
> 2. putting the reference of the class into the client -> linker says 'no
>    reference: Accel'.
> 
> I thought of delaring it virtual, but this doesn't work.
> Logicly, I must put the reference into the client, because I will load
> it.
> But how can I declare the class virtual, for the host.

You cannot do a "new" of a class in your main program if the class code
is in a DSO. The trick here is to have a simple factory function. Like
this:

(library code)

class Foo {
  ...
};

// the extern "C" is to avoid the mangling when we do the dlsym
// check out the library with "nm" and "nm -C" for further info
extern "C" Foo* newFoo() {
  return new Foo;
}

(main program code)

Foo* (*newFoo)();
Foo* foo;

newFoo = dlsym(dlh, "newFoo");
foo = newFoo();

-- 
Pierre Phaneuf
Ludus Design, http://ludusdesign.com/
"First they ignore you. Then they laugh at you.
Then they fight you. Then you win." -- Gandhi