9

I have a C++ class and I'm compiling it with some C files.

I want to call a function which is defined in C++, actually in C++ class, so what am I going to do?

The following declarations to show what am I saying: there may there be syntax errors:

serial_comm.cpp

class MyClass {
    void sendCommandToSerialDevice(int Command, int Parameters, int DeviceId) {
         //some codes that write to serial port.
    }
}

external.c

int main(int argc, char ** argv) {
    //what am I going to write here?
}
13
  • 1
    Can't you just rename external.c into external.cpp and invoke the class's function?
    – Andy Prowl
    Feb 11, 2013 at 15:31
  • 1
    There is lots of that C files. Can't rename.
    – totten
    Feb 11, 2013 at 15:32
  • Well, how about MyClass::sendCommandToSerialDevice();?
    – user529758
    Feb 11, 2013 at 15:32
  • @H2CO3: That's not a static function, so you can't invoke it that way. But I guess I see your point.
    – Andy Prowl
    Feb 11, 2013 at 15:33
  • 1
    @EnesUnal: Well, if that function is not static, you will need an object to invoke it on. Does not need to be a new one, but some object must exist. Otherwise, mark the function as static.
    – Andy Prowl
    Feb 11, 2013 at 15:36

5 Answers 5

21

The common approach to this problem is providing a C wrapper API. Write a C function that takes a pointer to a MyClass object (as MyClass is not valid C, you will need to provide some moniker, simplest one is moving void* around) and the rest of the arguments. Then inside C++ perform the function call:

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) {
   static_cast<MyClass*>(myclass)->sendCommandToSerialDevice(cmd,params,id);
}

Then the C code uses the C api to create the object, call the function and release the object:

// C
void* myclass = MyClass_create();
MyClass_sendCommandToSerialDevice(myclass,1,2,3);
MyClass_release(myclass);
2
  • 1
    could you please look at stackoverflow.com/questions/43537587/… Thanks Apr 21, 2017 at 8:25
  • Hmm -- this is pretty intrusive for C. Let's say you have a C API who's interface is provided a nullary (e.g. f()). This would mean providing a new function declaration who's definition reference the object instance. It there any way to form nullary closures with references to global data? (i.e. given some instance x and method f), can I produce a nullary C function pointer f that calls x.m(), without a global reference to x?
    – user48956
    Apr 24, 2020 at 16:07
10

You'll have to pass an additional argument, with the address of the object to call the function on. Something like:

extern "C" void SendCommandToSerialDevice( void* object,
    int command, int parameters, int deviceId )
{
    static_cast<MyClass*>( object)->sendCommandToSerialDevice(
        command, parameters, deviceId );
}

main will, of course, have to find the instance of the class somehow.

EDIT:

Concerning some points brought up in other answers:

  1. In your example, you compile main as C. This is undefined behavior, and in practice could mean that your constructors will not be called on static objects. (If your code is in a DLL, you're OK. The standard doesn't say anything about DLL's, but in practice, they work.)

  2. If you're reporting errors by means of exceptions, then you'll have to change your signature to report them in some other way, and wrap your code to catch all exceptions, and convert them to the C convention. (Since your function has no return value, this is easily handled by means of a return code.)

10
  • 1
    Regarding concern 1) technically it's not main that does static initialization, but the runtime environment that ultimately calls main after performing the initialization. As long as you link your executable with a C++ runtime you should be fine. For example you could compile main.c with a C compiler, .e.g. cc -c main.c but then link the binary as C++, e.g. c++ -o exe main.o serial_comm.o
    – datenwolf
    Feb 11, 2013 at 15:57
  • 1
    Also when linking a program that contains C++ compilation units you actually can not avoid doing it through the C++ path, as the C++ compilation units will contain references to symbols that would be missing going just through the C linker path. This is a standard SNAFU when using libraries implemented in C++ but having a C frontend; trying to use those in a pure C program yields a ton of undefined references; the default solution is to call the linker in C++ mode.
    – datenwolf
    Feb 11, 2013 at 16:00
  • @datenwolf How the runtime does the initialization is implementation defined. C++ has a lot of restrictions compared to C, because in early implementations, the C++ compiler recognized the function main, and inserted the initialization code there. I suspect that most modern implementations use the same technique they utilise for DLLs, so it will probably work, but formally, it's undefined behavior. Feb 11, 2013 at 16:16
  • @datenwolf And I think you are confusing one implementation (g++?) with what the standard requires, and what has been done in other implementations. Feb 11, 2013 at 16:17
  • 1
    @datenwolf Having actually worked on similar projects, and knowing something of the environment where C++ was developed, doing some meta-magic with main was by far the simplest solution. Feb 11, 2013 at 18:00
2

If you want to do it correct

serial_comm_wrapper.h

#ifdef __cpluscplus
class MyClass;
extern "C" {
#else
struct MyClass;
typedef struct MyClass MyClass;
#endif

MyClass *MyClass_new();

void MyClass_sendCommandToSerialDevice(MyClass *instance, int Command, int Parameters, int DeviceId);

#ifdef __cpluscplus
}
#endif

serial_comm_wrapper.cc

#include "serial_comm_wrapper.h"
#include "serial_comm.hh"

MyClass *MyClass_new()
{
    return new MyClass();
}

void MyClass_sendCommandToSerialDevice(MyClass *instance, int Command, int Parameters, int DeviceId)
{
    instance->sendCommandToSerialDevice(command, Parameters, DeviceID);
}

external.c

#include "serial_comm_wrapper.h"

int main(int argc, char ** argv) {
     MyClass *instance = MyClass_new();
     MyClass_sendCommandToSerialDevice(instance, ...);
}
9
  • You can't use dynamic_cast on a void*. And how main gets its poionter to the object is another issue. Feb 11, 2013 at 15:40
  • You cannot dynamic_cast from void* Feb 11, 2013 at 15:41
  • @JamesKanze: Okay I corrected it. Using C style type casting now (I don't know if a static_cast or reinterpret_cast would be better suited).
    – datenwolf
    Feb 11, 2013 at 15:44
  • @datenwolf I use static_cast, but when casting from void*, both are guaranteed to have the same semantics. Feb 11, 2013 at 15:46
  • 1
    @JamesKanze: Didn't you notice my typedef? As for what happens on the C side: A C opaque pointer to a struct behaves no differently than a void*, so this is exactly the same as writing void* without the explicit casts. Also the C standard asserts that casting a void* to a qualified type pointer will not change the pointer's value. So basically this is just semantic sugar. But very nice sugar. On the C side the compiler sees just some pointer (with the value a C++ code created), which value it won't touch and can't dereference. But on the C++ side that pointer has actual meaning.
    – datenwolf
    Feb 11, 2013 at 16:26
1

You can't just go calling C++ code from C.

You will need to produce a C++ interface that can be called from C.

Something like this

 // interface.h

 #ifdef __cplusplus
 extern "C" {
 #endif

 void createMyclass();

 void callMyclassSendCommandToSerialDevice(int Command, int Parameters, int DeviceId);

 void destroyMyclass();

 #ifdef __cplusplus
 extern }
 #endif

Then you do this:

 static MyClass *myclass;

 void createMyclass()
 {
    try
    {
        myclass = new MyClass;
    }
    catch(...)
    {
       fprintf(stderr, "Uhoh, caught an exception, exiting...\n");
       exit(1);
    }
 }


 void callMyclassSendCommandToSerialDevice(int Command, int Parameters, int DeviceId)
 {
     // May need try/catch here. 
     myclass->sendCommandToSerialDevice(Command, Parameters, DeviceId);
 }

 void destroyMyclass()
 {
    delete myclass;
 }

Note that it's IMPERATIVE that you don't let "exceptions" through the wall to the C code, as that is definite undefined behaviour.

10
  • With regards to exceptions---it's a good thing to mention, but in general, you can count on them passing right through the C. (On the other hand, his example compiles the main in C, which could mean that constructors to static objects are not called.) Feb 11, 2013 at 15:42
  • @JamesKanze: See my comment your answer. It's not the compilation unit containing main, that does the static initialization, but the runtime library, that will eventually also call main. If your program gets linked with the C++ runtime you're fine. You can implement main in a C compilation unit just fine.
    – datenwolf
    Feb 11, 2013 at 16:02
  • Is that guaranteed, or just "most compilers do it that way"? Feb 11, 2013 at 16:05
  • @datenwolf Your comment is wrong. You just describe how one particular implementation does it. Most modern implementations probably do something similar, because they want to support C++ in DLLs, and need the technology, but I've used implementations which didn't. Feb 11, 2013 at 16:18
  • @JamesKanze: I know of no implementation where the main function would be the process entry point (unless you explicitly told the linker to do so). Even all the embedded system compilers I know, main is called through the runtime system. I know that at least the following implementations work that way. MSVC++, GNU C++, DMC++, Borland C++, Intel Compiler Suite and Open Watcom.
    – datenwolf
    Feb 11, 2013 at 16:25
0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.