golang/go · error

C compiler %q not found: %v

Error message

C compiler %q not found: %v

What it means

Returned by the cgo CC resolver (gcc.go:1752) when exec.LookPath fails for the resolved C compiler name. The compiler name comes from the CC env var or defaultCC(goos, goarch); LookPath searches PATH and errors if no executable matches. This blocks cgo from compiling C/C++ sources.

Source

Thrown at src/cmd/cgo/gcc.go:1752

func checkGCCBaseCmd() ([]string, error) {
	// Use $CC if set, since that's what the build uses.
	value := os.Getenv("CC")
	if value == "" {
		// Try $GCC if set, since that's what we used to use.
		value = os.Getenv("GCC")
	}
	if value == "" {
		value = defaultCC(goos, goarch)
	}
	args, err := quoted.Split(value)
	if err != nil {
		return nil, err
	}
	if len(args) == 0 {
		return nil, errors.New("CC not set and no default found")
	}
	if _, err := exec.LookPath(args[0]); err != nil {
		return nil, fmt.Errorf("C compiler %q not found: %v", args[0], err)
	}
	return args[:len(args):len(args)], nil
}

// gccMachine returns the gcc -m flag to use, either "-m32", "-m64" or "-marm".
func gccMachine() []string {
	switch goarch {
	case "amd64":
		if goos == "darwin" {
			return []string{"-arch", "x86_64", "-m64"}
		}
		return []string{"-m64"}
	case "arm64":
		if goos == "darwin" {
			return []string{"-arch", "arm64"}
		}
	case "386":
		return []string{"-m32"}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Install a C compiler: apt-get install gcc / brew install gcc / dnf install gcc.
  2. Set CC explicitly to a known-good compiler: `CC=gcc go build` or `CC=/usr/local/bin/gcc-13 go build`.
  3. If you do not need cgo, set CGO_ENABLED=0 to skip C compilation entirely.
  4. Verify with `which gcc` / `which clang` and that the CC env var (if set) points to an existing executable.

Example fix

// before
$ go build ./mypkg  // uses cgo, CC unset, no gcc on PATH -> error

// after (option A: install compiler)
$ sudo apt-get install -y gcc && go build ./mypkg

// after (option B: disable cgo)
$ CGO_ENABLED=0 go build ./mypkg

// after (option C: point CC)
$ CC=/opt/homebrew/bin/gcc-13 go build ./mypkg
Defensive patterns

Strategy: validation

Validate before calling

func cgoCompilerAvailable() error {
    cc := os.Getenv("CC")
    if cc == "" {
        cc = "gcc" // defaultCC fallback
    }
    if _, err := exec.LookPath(cc); err != nil {
        return fmt.Errorf("C compiler %q not on PATH: %w", cc, err)
    }
    return nil
}

Prevention

When it happens

Trigger: Building a package that uses cgo (imports "C") with CGO_ENABLED=1, where CC is unset and the default compiler (gcc/clang) is not on PATH, or where CC names a binary that does not exist. LookPath returns an error which is wrapped here.

Common situations: Fresh container/CI image without gcc/clang installed; CC set to a cross-compiler that is not installed; PATH missing /usr/bin or Homebrew's bin; building on a minimal alpine image without build-base; CC points to a typo'd path.

Related errors


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