{"record":{"id":"6018e00f4c681326","repo":"golang/go","slug":"c-source-files-not-allowed-when-not-using-cgo-or","errorCode":null,"errorMessage":"C++ source files not allowed when not using cgo or SWIG: %s","messagePattern":"C\\+\\+ source files not allowed when not using cgo or SWIG: (.+?)","errorType":"exception","errorClass":"PackageError","httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/load/pkg.go","lineNumber":2111,"sourceCode":"\t\tp.MFiles = nil\n\t\tp.SwigFiles = nil\n\t\tp.SwigCXXFiles = nil\n\t\t// Note that SFiles are okay (they go to the Go assembler)\n\t\t// and HFiles are okay (they might be used by the SFiles).\n\t\t// Also Sysofiles are okay (they might not contain object\n\t\t// code; see issue #16050).\n\t}\n\n\t// The gc toolchain only permits C source files with cgo or SWIG.\n\tif len(p.CFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() && cfg.BuildContext.Compiler == \"gc\" {\n\t\tsetError(fmt.Errorf(\"C source files not allowed when not using cgo or SWIG: %s\", strings.Join(p.CFiles, \" \")))\n\t\treturn\n\t}\n\n\t// C++, Objective-C, and Fortran source files are permitted only with cgo or SWIG,\n\t// regardless of toolchain.\n\tif len(p.CXXFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {\n\t\tsetError(fmt.Errorf(\"C++ source files not allowed when not using cgo or SWIG: %s\", strings.Join(p.CXXFiles, \" \")))\n\t\treturn\n\t}\n\tif len(p.MFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {\n\t\tsetError(fmt.Errorf(\"Objective-C source files not allowed when not using cgo or SWIG: %s\", strings.Join(p.MFiles, \" \")))\n\t\treturn\n\t}\n\tif len(p.FFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {\n\t\tsetError(fmt.Errorf(\"Fortran source files not allowed when not using cgo or SWIG: %s\", strings.Join(p.FFiles, \" \")))\n\t\treturn\n\t}\n}\n\n// An EmbedError indicates a problem with a go:embed directive.\ntype EmbedError struct {\n\tPattern string\n\tErr     error\n}\n","sourceCodeStart":2093,"sourceCodeEnd":2129,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/load/pkg.go#L2093-L2129","documentation":"A Go package contains C++ source files (.cc, .cpp, .cxx) but neither cgo nor SWIG is active. Unlike the C-file check which only applies to the gc compiler, this restriction applies to all toolchains (both gc and gccgo). The check is len(p.CXXFiles) > 0 && !p.UsesCgo() && !p.UsesSwig().","triggerScenarios":"Adding .cpp or .cc files to a package directory without cgo or SWIG integration. Having leftover C++ files after removing cgo integration. Building with CGO_ENABLED=0 while C++ files remain.","commonSituations":"Mixing C++ and Go without proper cgo setup. Accidental inclusion of C++ files in a Go package directory. Disabling cgo while C++ files remain. Third-party dependencies that ship C++ files without proper cgo annotations.","solutions":["Enable cgo and add a // #cgo CXXFLAGS preamble in a .go file that wraps the C++ code with extern \"C\".","Ensure CGO_ENABLED=1.","Remove the C++ files if they are not needed for the build.","Use SWIG (.swigcxx file) for complex C++ class integration."],"exampleFix":"// before — .cpp file present but no cgo\n// mypackage/\n//   main.go\n//   helper.cpp\n\n// after — add cgo with C++ support\n// mypackage/main.go\npackage main\n\n/*\n#cgo CXXFLAGS: -std=c++11\n#cgo LDFLAGS: -lstdc++\n#include \"helper.h\"\n*/\nimport \"C\"\n// mypackage/helper.cpp with extern \"C\" wrapper remains","handlingStrategy":"validation","validationCode":"// Check that C++ files are accompanied by cgo or SWIG before building.\nfunc checkCxxCgoRequirement(pkgDir string) error {\n    entries, _ := os.ReadDir(pkgDir)\n    var hasCxx, hasCgo, hasSwig bool\n    for _, e := range entries {\n        name := e.Name()\n        for _, suffix := range []string{\".cc\", \".cpp\", \".cxx\"} {\n            if strings.HasSuffix(name, suffix) {\n                hasCxx = true\n            }\n        }\n        if strings.HasSuffix(name, \".swig\") || strings.HasSuffix(name, \".swigcxx\") {\n            hasSwig = true\n        }\n        if strings.HasSuffix(name, \".go\") {\n            data, _ := os.ReadFile(filepath.Join(pkgDir, name))\n            if bytes.Contains(data, []byte(\"import \\\"C\\\"\")) {\n                hasCgo = true\n            }\n        }\n    }\n    if hasCxx && !hasCgo && !hasSwig {\n        return fmt.Errorf(\"C++ files present without cgo or SWIG\")\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set up cgo with #cgo CXXFLAGS whenever adding C++ source files.","Wrap C++ code with extern \\\"C\\\" interfaces for cgo interoperability.","Ensure CGO_ENABLED=1 when building packages with C++ files.","Consider SWIG (.swigcxx) for complex C++ class integration."],"tags":["go","go-build","cgo","cpp","cxx","native-code"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}