{"record":{"id":"db81cd20b9f541e0","repo":"golang/go","slug":"c-source-files-not-allowed-when-not-using-cgo-or-s","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":2104,"sourceCode":"\t}\n\n\t// If cgo is not enabled, ignore cgo supporting sources\n\t// just as we ignore go files containing import \"C\".\n\tif !cfg.BuildContext.CgoEnabled {\n\t\tp.CFiles = nil\n\t\tp.CXXFiles = nil\n\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}","sourceCodeStart":2086,"sourceCodeEnd":2122,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/load/pkg.go#L2086-L2122","documentation":"A Go package contains .c files but no .go file has 'import \"C\"' (cgo) and no SWIG (.swig/.swigcxx) files are present. The gc compiler toolchain only allows C source files when cgo or SWIG is active. This check is specific to cfg.BuildContext.Compiler == \"gc\"; gccgo handles C files differently. The p.UsesCgo() and p.UsesSwig() methods determine whether cgo or SWIG is active.","triggerScenarios":"Adding a .c file to a Go package directory without also having cgo (import \"C\" in a .go file) or SWIG interface files. Cgo is disabled via CGO_ENABLED=0 but .c files remain in the directory. Removing import \"C\" from Go files while leaving C files in place.","commonSituations":"Adding C helper files without setting up cgo properly. Setting CGO_ENABLED=0 (common in cross-compilation or minimal containers) while C files remain. Removing cgo integration from a package but forgetting to delete the C files. Accidental inclusion of C files from a third-party dependency.","solutions":["If you need the C files, add cgo to the package: create a .go file with import \"C\" and a // #cgo or // #include preamble.","Ensure CGO_ENABLED=1 in your build environment (check with: go env CGO_ENABLED).","If you don't need the C files, remove them from the package directory.","If using SWIG, add the appropriate .swig or .swigcxx file."],"exampleFix":"// before — .c file present but no cgo\n// mypackage/\n//   main.go\n//   helper.c\n\n// after — add cgo preamble to a .go file\n// mypackage/main.go\npackage main\n\n/*\n#include \"helper.c\"\n*/\nimport \"C\"\n\nfunc main() {}\n// mypackage/helper.c remains","handlingStrategy":"validation","validationCode":"// Check that C files are accompanied by cgo or SWIG before building.\nfunc checkCgoRequirement(pkgDir string) error {\n    entries, _ := os.ReadDir(pkgDir)\n    var hasC, hasCgo, hasSwig bool\n    for _, e := range entries {\n        name := e.Name()\n        if strings.HasSuffix(name, \".c\") {\n            hasC = true\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 hasC && !hasCgo && !hasSwig {\n        return fmt.Errorf(\"C files present without cgo or SWIG — add import \\\"C\\\" or set CGO_ENABLED=1\")\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set up cgo properly (import \"C\" in a .go file) whenever adding C source files.","Ensure CGO_ENABLED=1 when building packages with native source files.","Remove C files when cgo is not needed for the build.","Use go env CGO_ENABLED to verify the setting before building."],"tags":["go","go-build","cgo","c","gc-compiler","native-code"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}