c++ccross-language

How to handle a C++ header file with a extern call to C function and a class object


I am trying to compile an application involving both C and C++ files. With one particular header I face issues. The file in question (a C++ header file), would look something like this:

#ifndef TASK_H
#define TASK_H

#include "MyCCPObject.h"

int foo1(int);
int foo2(int);
int fooObject(MyCCPObject myCppObject); // Function involves a Class "MyCCPObject" type

#ifdef __cplusplus
extern "C" {
#endif
    int foo3(void); // Function called in a C file
#ifdef __cplusplus
}
#endif

#endif //TASK_H

I have a function fooObject() which has a MyCCPObject class type as a parameter. Also, one of the functions, foo3() would be called from a C file. When the C compiler, compiles this header, I get the following error: "error: #20:identifier "class" is undefined". To avoid this, I had to:

  1. Place the fooObject() declaration within compiler guards:
#ifdef __cplusplus
int fooObject(MyCCPObject myCppObject);
#endif
  1. Place compiler guards also in the class declaration in the header file MyCCPObject.h:
#ifdef __cplusplus
class MyCCPObject
{
public:
    MyCCPObject(uint32_t val);
private:
    uint32_t value;

};
#endif

Note: The MyCCPObject would not be called in any C file. So, what would be a better approach, when I have a C++ header file, which involves:

  1. Function would involves a class object
  2. A extern call to a C file

Solution

  • The makers of C++ wrote a FAQ which is also giving some guidance on how to mix C and C++. They are also looking at the possibility to use C++ objects from C code.

    Option 1: If you just want the C compiler to be able to parse your task.h header file, then you could hide the C++ parts my using #ifdef __cplusplus:

    #ifndef TASK_H
    #define TASK_H
    
    #ifdef __cplusplus
    #include "MyCCPObject.h"
    
    int foo1(int);
    int foo2(int);
    int fooObject(MyCCPObject myCppObject); // Function involves a Class "MyCCPObject" type
    
    extern "C" {
    #endif
        int foo3(void); // Function called in a C file
    #ifdef __cplusplus
    }
    #endif
    
    #endif //TASK_H
    

    Option 2: If you want to make the fooObject function accessible from C, then you can change MyCppObject.h to provide the full class information to C++ and only a minimal typedef for C. The typedef makes sure that C understands just the class name MyCCPObject without writing class or struct before it.

    #ifdef __cplusplus
    class MyCCPObject
    {
    public:
        MyCCPObject(uint32_t val);
    private:
        uint32_t value;
    
    };
    #else
    typedef struct MyCCPObject MyCCPObject;
    #endif
    

    and task.h to

    #ifndef TASK_H
    #define TASK_H
    
    #include "MyCCPObject.h"
    
    int foo1(int);
    int foo2(int);
    
    #ifdef __cplusplus
    extern "C" {
    #endif
        int fooObject(MyCCPObject *myCppObject); // Function involves a Class "MyCCPObject" type
        int foo3(void); // Function called in a C file
    #ifdef __cplusplus
    }
    #endif
    
    #endif //TASK_H
    

    Please note that I needed to change the signature of fooObject to take a pointer to the object as the C code does not see the complete class and does not know the size of the object.