golang/go · error

CC not set and no default found

Error message

CC not set and no default found

What it means

Thrown by checkGCCBaseCmd when no C compiler can be determined: $CC is empty, $GCC is empty, defaultCC(goos,goarch) returned an empty string, and splitting that empty value produced zero arguments. cgo requires a working C compiler to invoke gcc for parsing C debug info.

Source

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

//
// checkGCCBaseCmd confirms that the compiler exists in PATH, returning
// an error if it does not.
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"}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Set the CC environment variable to an absolute path of an installed C compiler, e.g. export CC=/usr/bin/gcc.
  2. If you do not need cgo, build with CGO_ENABLED=0.
  3. Install gcc or clang (e.g. apt-get install build-essential) and retry.
  4. Verify the compiler is on PATH with `exec.LookPath` semantics (which checkGCCBaseCmd uses next).

Example fix

// before (no compiler, no default)
go build ./...
// after
export CC=/usr/bin/gcc
go build ./...
Defensive patterns

Strategy: validation

Validate before calling

// Ensure a C compiler is reachable before invoking cgo.
import "os/exec"
func resolveCC() (string, error) {
    for _, v := range []string{os.Getenv("CC"), os.Getenv("GCC"), "gcc", "clang", "cc"} {
        if v == "" { continue }
        if _, err := exec.LookPath(v); err == nil { return v, nil }
    }
    return "", errors.New("no C compiler found")
}

Prevention

When it happens

Trigger: Building a package that uses cgo (imports "C") on a system where no CC/GCC environment variable is set and the Go build has no compiled-in default compiler for the GOOS/GOARCH pair. Practically, this means the toolchain's defaultCC returned "" — e.g. an unusual target with no default.

Common situations: Cross-compiling to an exotic target without setting CC; a stripped/minimal toolchain build where defaultCC returns empty; CI image that removes gcc/clang and clears CC; building with CGO_ENABLED=1 but no compiler installed and no default defined.

Related errors


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