I'm trying to include a (.h) header file which is auto-generated by some compiler in my code. Below is the code snip from auto-generated header file.
typedef struct SequenceOfUint8 { // Line 69
struct SequenceOfUint8 *next;
Uint8 value;
} *SequenceOfUint8; // Line 72
If I include this header file in C code (gcc compiler), it compiles fine without any error, but if try to include this in CPP code, g++ compiler throws below mentioned error.
In file included from ssme/src/../include/xxxxx.h:39:0,
from ssme/src/ssme.cpp:11:
ssme/src/../include/yyyyy.h:72:4: error: conflicting declaration ‘typedef struct SequenceOfUint8* SequenceOfUint8’
} *SequenceOfUint8;
^~~~~~~~~~~~~~~
ssme/src/../include/yyyyy.h:69:16: note: previous declaration as ‘struct SequenceOfUint8’
typedef struct SequenceOfUint8 {
^~~~~~~~~~~~~~~
Can someone please tell how to use this in C++ code (if possible without changing the auto-generated code).
PS: I included the header file in CPP file using extern "C" { #include "yyyy.h" } statement, still no luck.
You can't use it as is in C++ code. This is another instance where C and C++ being two different languages matters.
The tag namespace in C is separate, in C++ it isn't. It doesn't even exist in C++ to be precise.
Wrapping in extern "C" is also not going to make a C++ compiler treat the header as C code. That's not the intended function. The header must be standalone valid C++, which it simply isn't.
You will need to write a C wrapper, that exposes a C++ compatible API.
Disclaimer: use this answer at your own risk. I'm not your mom.
Supposing that:
typedef inside the generated file// <Your lenghty apologetic comment here>
#define typedef static
#include <generated.h>
#undef typedef
This will replace all typedefs with static variables, which can hide types. Note that you'll need to use the elaborated type name struct SequenceOfUint8 to refer to the types later in the same file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With