0

I have a C++ header file, which I am including in a project running with a C compiler. and I have a few errors in the C++ header file since the syntaxes are different for both the compilers, obviously. But the hurdle is I should not modify the c++ header file. I tried using 'C' extern after reading from a few posts but seems the C compiler is not recognizing "__cplusplus".

is there any way that can be implemented in a C header file to include a C++ header file??

for eg. the C compiler is giving an error "Identifier 'Current' is undefined", because there is a 'struct' keyword missing in front of 'Current' in below code.

struct Current {
    uint16_t mini; 
    uint16_t maxi; 
    uint16_t counter; 
};  
struct someConstraint { 
    union { 
        Current ac_curr; 
    };
};

Please help and thank you !!

VP

273K
  • 29,503
  • 10
  • 41
  • 64
  • 5
    What do you expect to accomplish for your C program, by including the C++ header file, in the first place? C++ is a completely different language from C. No C compiler will make any sense of C++ class declarations, or any C++ specific grammar. – Sam Varshavchik Jun 13 '22 at 14:57
  • I am working with a C compiler, but there is a dependency on the header file which is compiled well in C++ and I have no choice but to include it. to give an example: struct Current { uint16_t min; uint16_t max; uint16_t ctr; }; struct someConstraint { uint8_t type; union { Current ac_current; }; }; the C compiler is giving an error "Identifier 'Current' is undefined", because there is a 'struct' keyword missing in front of 'Current'. – Vidhi Patel Jun 13 '22 at 15:02
  • 2
    A C header file can include a C++ header file if the C++ header file has taken steps to be C compatible. If you cannot modify the C++ header file, and the C++ header file is not C compatible, then you can't use it. At best, you can write a separate header file that is C compatible that is the subset of the C++ header file that needs to be shared with C. (Those identifiers still need to be C naming friendly, and not C++ mangled.) – Eljay Jun 13 '22 at 15:02
  • 1
    Please post (a) some of the errors and (b) the parts of the C++ header file giving the errors posted in (a). Please read [ask] with a [mcve]. – Richard Critten Jun 13 '22 at 15:05
  • You have to write a C header. And use `#ifdef __cplusplus extern "C" { #endif ... #ifdef __cplusplus }; #endif` so it works right in C++. Also in C you need `struct Current ac_curr;` or a `typedef`. – Goswin von Brederlow Jun 13 '22 at 15:20
  • 2
    I'm not aware of any C compiler that has an option to read C++ code and understand it. If it did, it would be called a "C++ compiler". – Sam Varshavchik Jun 13 '22 at 15:31
  • and I get it but I am trying to have the work around if possible. – Vidhi Patel Jun 13 '22 at 15:34
  • The work around is to rewrite the header file in the language you are using. There is not a general way to mix arbitrary C++ code with arbitrary C code in the same translation unit, as they are incompatible languages. – Drew Dormann Jun 13 '22 at 15:36
  • FYI: [Developing C wrapper API for Object-Oriented C++ code](https://stackoverflow.com/q/2045774/7478597) – Scheff's Cat Jun 13 '22 at 16:44

1 Answers1

0

ISO C N2176 §6.2.2 Linkages of identifiers

  1. For an identifier […] If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage.

You can typedef that struct:

typedef struct Current Current;
#include "somelib.h" // defines struct Current

Example on compiler explorer

viraltaco_
  • 814
  • 5
  • 14