Extern C return class object

I want to have a plugin with a simpler name to solve in another C ++ code.

class B {
};

extern "C" B foo(); // to avoid name mangling in order to be loaded by dlsym

And in another part of the program (which is also in C ++ and has the same class B definition with the plugin):

B (*func)();
func = dlsym("/path/to/so", "foo");
B m = func();

Will such code cause any problems, that is, it is allowed (by standard) to use the C ++ class as a parameter or return type in a function extern "C"? This seems to work on my gcc, but what about the others?

+5
source share
3 answers

This should work with several conditions:

  • If you intend to switch the definition of class B to something else, this will not work. The only thing you can change is the definition of foo ().
  • B . ( ) .
  • dlsym() ++.
  • C .
+2

foo() extern "C", , dlsym() , , , .

. foo(), B, , , , .

+1

It will work as long as you stick with C ++ and only C ++. For obvious reasons, you cannot compile a function declaration in a C-translation block. (Ie you can never properly explain the C compiler with what it Bis.)

So, the only problem I see here is that the value of the [C] tag in your question. Do you also need some kind of cross-compatibility with C?

0
source

All Articles