Generate extern "C" in the .c, not just the .h.

This commit is contained in:
Nigel Tao committed 2017-05-27 09:24:31 +10:00
1 parent 71a3ad6563
commit cc9aeb63c1
2 files changed
+21 -17

No files matched your search

+13 -17
View File
@@ -26,10 +26,7 @@ func (g *Generator) Generate(pkgName string, m *t.IDMap, files []*a.File) ([]byt
// Write preamble. // Write preamble.
fmt.Fprintf(b, "// Code generated by puffs-gen-%c. DO NOT EDIT.\n\n", g.Extension) fmt.Fprintf(b, "// Code generated by puffs-gen-%c. DO NOT EDIT.\n\n", g.Extension)
b.WriteString(preamble) b.WriteString(preamble)
b.WriteByte('\n') b.WriteString("\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n")
if g.Extension == 'h' {
b.WriteString("#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n")
}
b.WriteString("// ---------------- Status Codes\n\n") b.WriteString("// ---------------- Status Codes\n\n")
b.WriteString("typedef enum {\n") b.WriteString("typedef enum {\n")
@@ -60,24 +57,23 @@ func (g *Generator) Generate(pkgName string, m *t.IDMap, files []*a.File) ([]byt
} }
} }
if g.Extension != 'h' { // Finish up the header, which is also the first part of the .c file.
b.WriteString("// ---------------- Constructor and Destructor Implementations\n\n") b.WriteString("\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n")
for _, f := range files { if g.Extension == 'h' {
for _, n := range f.TopLevelDecls() { return format(b)
if n.Kind() == a.KStruct { }
if err := writeCtorImpls(b, pkgName, m, n.Struct()); err != nil {
return nil, err b.WriteString("// ---------------- Constructor and Destructor Implementations\n\n")
} for _, f := range files {
for _, n := range f.TopLevelDecls() {
if n.Kind() == a.KStruct {
if err := writeCtorImpls(b, pkgName, m, n.Struct()); err != nil {
return nil, err
} }
} }
} }
} }
// Write postamble.
if g.Extension == 'h' {
b.WriteString("\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n")
}
return format(b) return format(b)
} }
+8
View File
@@ -15,6 +15,10 @@
#error "Puffs requires a word size of at least 32 bits" #error "Puffs requires a word size of at least 32 bits"
#endif #endif
#ifdef __cplusplus
extern "C" {
#endif
// ---------------- Status Codes // ---------------- Status Codes
typedef enum { typedef enum {
@@ -39,6 +43,10 @@ void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self);
void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self); void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self);
#ifdef __cplusplus
} // extern "C"
#endif
// ---------------- Constructor and Destructor Implementations // ---------------- Constructor and Destructor Implementations
void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self) { void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self) {