]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: wrap generated exports with extern "C" for C++
authorIan Lance Taylor <iant@golang.org>
Thu, 7 May 2015 19:58:43 +0000 (12:58 -0700)
committerIan Lance Taylor <iant@golang.org>
Fri, 8 May 2015 04:23:43 +0000 (04:23 +0000)
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 <minux@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/cgo/out.go

index 30f828c4e919f494cd07f37f2690d5b314dc6cc5..87f21ed822b99ca5fce778c7fdb379691f59dbfc 100644 (file)
@@ -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