golang/go · error

%s: C source files not supported without cgo

Error message

%s: C source files not supported without cgo

What it means

The gcToolchain.cc method - the default gc compiler's C-compilation stub - unconditionally returns this error. It means a package contains .c (or other C source) files but CGO_ENABLED=0, so the gc toolchain has no way to compile them. The path is made absolute via mkAbs(a.Package.Dir, cfile) for clarity in the message.

Source

Thrown at src/cmd/go/internal/work/gc.go:728

	// On OS X when using external linking to build a shared library,
	// the argument passed here to -o ends up recorded in the final
	// shared library in the LC_ID_DYLIB load command.
	// To avoid putting the temporary output directory name there
	// (and making the resulting shared library useless),
	// run the link in the output directory so that -o can name
	// just the final path element.
	// On Windows, DLL file name is recorded in PE file
	// export section, so do like on OS X.
	// On Linux, for a shared object, at least with the Gold linker,
	// the output file path is recorded in the .gnu.version_d section.
	dir, targetPath := filepath.Split(targetPath)

	return b.Shell(root).run(dir, targetPath, cfgChangedEnv, cfg.BuildToolexec, base.Tool("link"), "-o", targetPath, "-importcfg", importcfg, ldflags)
}

func (gcToolchain) cc(b *Builder, a *Action, ofile, cfile string) error {
	return fmt.Errorf("%s: C source files not supported without cgo", mkAbs(a.Package.Dir, cfile))
}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Enable cgo: `CGO_ENABLED=1 go build` (requires a working C compiler)
  2. Remove the C source files or the dependency that brings them in
  3. Add build tags to exclude C files for non-cgo builds

Example fix

// before
CGO_ENABLED=0 go build ./...
// after
CGO_ENABLED=1 go build ./...
Defensive patterns

Strategy: validation

Validate before calling

// Before a CGO_ENABLED=0 build, check for C source files
if os.Getenv("CGO_ENABLED") == "0" {
    if matches, _ := filepath.Glob("*.c"); len(matches) > 0 {
        return fmt.Errorf("C files present but cgo disabled: %v", matches)
    }
}

Prevention

When it happens

Trigger: Fires in gcToolchain.cc when the build attempts to compile a C source file in a package while cgo is disabled (CGO_ENABLED=0 or CGO_ENABLED unset with no C compiler).

Common situations: Cross-compiling with CGO_ENABLED=0 while depending on a package with C sources, or building in a minimal container (scratch/alpine) without a C compiler.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/ffe404e649bd38ba. Report an issue: GitHub.