From e8fc93ea45ca0147f24f61f5ed48e68b57d473df Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Thu, 7 May 2015 12:58:43 -0700 Subject: [PATCH] cmd/cgo: wrap generated exports with extern "C" for C++ This will make it possible for C++ code to #include the export header file and see the correct declarations. The preamble remains the user's responsibility. It would not be appropriate to wrap the preamble in extern "C", because it might include header files that work with both C and C++. Putting those header files in an extern "C" block would break them. Change-Id: Ifb40879d709d26596d5c80b1307a49f1bd70932a Reviewed-on: https://go-review.googlesource.com/9850 Reviewed-by: Minux Ma Run-TryBot: Ian Lance Taylor Reviewed-by: David Crawshaw TryBot-Result: Gobot Gobot --- src/cmd/cgo/out.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go index 30f828c4e9..87f21ed822 100644 --- a/src/cmd/cgo/out.go +++ b/src/cmd/cgo/out.go @@ -846,6 +846,8 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) { fmt.Fprint(fgo2, "}\n") } } + + fmt.Fprintf(fgcch, "%s", gccExportHeaderEpilog) } // Write out the C header allowing C code to call exported gccgo functions. @@ -1009,6 +1011,8 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) { fmt.Fprint(fgo2, ")\n") fmt.Fprint(fgo2, "}\n") } + + fmt.Fprintf(fgcch, "%s", gccExportHeaderEpilog) } // writeExportHeader writes out the start of the _cgo_export.h file. @@ -1374,6 +1378,17 @@ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; #endif /* End of boilerplate cgo prologue. */ + +#ifdef __cplusplus +extern "C" { +#endif +` + +// gccExportHeaderEpilog goes at the end of the generated header file. +const gccExportHeaderEpilog = ` +#ifdef __cplusplus +} +#endif ` // gccgoExportFileProlog is written to the _cgo_export.c file when -- 2.48.1