You cannot invoke a C++ method directly in C. Instead you may create a C wrapper and then call that:
C/C++ compatible header file:
#ifdef __cplusplus
extern "C" {
#endif
struct MyClass;
MyClass *new_MyClass();
void MyClass_sendCommandToSerialDevice(MyClass *c, int Command, int Parameters, int DeviceID);
#ifdef __cplusplus
} // extern "C"
#endif
implementation (a .cpp file)
#include "my_c_compatiblity_header.h"
#include "MyClass.h"
extern "C" MyClass *new_MyClass() { return new MyClass(); }
extern "C"
void MyClass_sendCommandToSerialDevice(MyClass *c, int Command, int Parameters, int DeviceID) {
c->sendCommandToSerialDevice(Command, Parameters, DeviceID);
}
main.c
int main(int argc, char ** argv) {
MyClass *c = new_MyClass();
MyClass_sendCommandToSerialDevice(c, 1, 0, 123);
}
Of course since resource and error handling can be different between C and C++ code you'll have to work out how to handle the combination in your case. For example the above just leaks a MyClass
object instead of cleaning up, and doesn't do anything about exceptions.