[GSoC] Header Generation for C/C++

Manu turkeyman at gmail.com
Wed Jul 17 17:40:35 UTC 2019


On Wed, Jul 17, 2019 at 5:55 AM Eduard Staniloiu via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
>
> On Wednesday, 17 July 2019 at 11:52:17 UTC, Nicholas Wilson wrote:
> > On Wednesday, 17 July 2019 at 11:05:21 UTC, Eduard Staniloiu
> > wrote:
> >> On Tuesday, 16 July 2019 at 13:46:56 UTC, rikki cattermole
> >> wrote:
> >>> So currently there is no way to restrict it to just extern(C)?
> >>> I ask this because -HC makes me think C not C++ headers.
> >>
> >> Currently the outputted headers are C++ headers, but we were
> >> thinking of wrapping `extern (C++)` definitions inside an
> >> `#ifdef __cplusplus` block, and prefixing any `extern (C)`
> >> definitions with the following `EXTERNC` macro
> >>
> >> ```
> >> #ifdef __cplusplus
> >> #define EXTERNC extern(C)
> >> #else
> >> #define EXTERNC
> >> #endif
> >> ```
> >>
> >> This way, the generated header could be used in both C and C++.
> >>
> >> What do you think?
> >
> > It should be pretty trivial to just check the linkage of the
> > symbols and only output extern(C) symbols.
>
> This would require two compiler switches, one for C (-HC ?) and
> one for C++ (-HCPP ?).
> Was trying to keep it as one compiler switch.

No, just emit one header with all the normal guards for C compilers.

#ifdef __cplusplus
# define EXTERN_C extern "C"
#else
# define EXTERN_C
#endif

// C declarations
EXTERN_C void cfunc();
EXTERN_C void cfunc2();
...

// C++ declarations:
#ifdef __cplusplus
void cppFunc();
...
#endif


If you detect that there are no C++ symbols at all, then you could simplify to:

#ifdef __cplusplus
extern "C" {
#endif

// C declarations
void cFunc();
...

#ifdef __cplusplus
}
#endif


More information about the Digitalmars-d mailing list