vendredi 21 avril 2017

C function calling C++ member function - where the C code is compiled with a C compiler

PLEASE before closing as dupe, read the question & see why it is different (hint: it's the C compiler)

I have Googled and found many, many, explanations of how a C function can call a C++ member function.

They all look similar to the accepted answer to this question, from a very high rep member.

It says

In a header file, put

extern "C" void* MyClass_create() {
   return new MyClass;
extern "C" void MyClass_release(void* myclass) {
   delete static_cast<MyClass*>(myclass);
extern "C" void MyClass_sendCommandToSerialDevice(void* myclass, int cmd, int params, int id) {

and, in the C code, put

void* myclass = MyClass_create();

That seems straightforward, but what I don't understand is that the header file is going to have to reference MyClass (never mind that static_cast), but I want to compile my C code with a C compiler (gcc), not a C++ compiler (g++).

It won't work. How can I call a C++ member function from C code - which is compiled with a C compiler?

@DavidRodríguez-dribeas, can you help?


0 commentaires:

Enregistrer un commentaire