What you want is simply not possible. C does not understand C++ class definitions nor C++ object layout. Thus, there is no way a C compiler could compute the size of a C++ class…
I think your best bet here would be to turn the problem around. While C does not understand C++ classes, C++ does understand C structs (to some degree) and can link with C. Instead of a C++ class, define a C struct as well as functions that operate on objects of that type. These functions can be implemented and used across both, C and C++:
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
int a;
float p[3];
} SomeThing;
void SomeThing_construct(SomeThing* obj, int arg);
void SomeThing_destruct(SomeThing* obj);
void SomeThing_doYourThing(const SomeThing* obj, float x);
#ifdef __cplusplus
}
#endif
As pointed out by n314159, you could then add a wrapper class around this basic interface to use on the C++ side of things.
I would try to avoid going down the code generation path unless absolutely necessary. For code generation to compute the size of a C++ class means that it has to invoke a C++ compiler at some point. Preferably the exact compiler that will be used to compile the rest of the code with exactly the same flags that will be used to compile the rest of the code (C++ object layout does generally not just depend on the compiler but may even be affected by certain compiler flags). You will want to write a tool that generates a header file that contains the code you need. You will want to integrate the compilation and running of this tool into your buildsystem. Think of the generated header file as a dependency that is to be built like, e.g., a library would be. How you would go about doing this exactly depends on your build system. In a Makefile, you can just add a target to build the header file. If you use CMake to generate your buildsystem, you would add your tool using add_executable
, then use add_custom_command()
to define how the header file is to be built, and then add the generated header file to the sources of a target…