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:
- Place the
fooObject()
declaration within compiler guards:
#ifdef __cplusplus
int fooObject(MyCCPObject myCppObject);
#endif
- 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:
- Function would involves a class object
- A
extern
call to a C file