Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixed C/C ++ source: Get sizeof(C++ struct) in C code

In this project, I need to get the size of a struct in a header file from within a C file.

I can't include the header file in the C file because the struct contains classes which will not compile in C.

Any ideas?

like image 587
eplictical Avatar asked Mar 27 '26 20:03

eplictical


1 Answers

You could declare in the .h file:

extern const size_t SIZE_OF_MY_STRUCT;

And define SIZE_OF_MY_STRUCT in the .cpp file as:

extern const size_t SIZE_OF_MY_STRUCT = sizeof(MyStruct);

So you would not have the overhead of a function call.

like image 93
kay Avatar answered Mar 29 '26 09:03

kay