Skip to content
Latchkey

Go "C source files not allowed when not using cgo" - Fix in CI

A package contains C source files, but the build has cgo disabled. Go cannot compile .c files without cgo, so rather than ignore them it fails with C source files not allowed when not using cgo.

What this error means

A build stops with C source files not allowed when not using cgo: foo.c for a package that ships C alongside its Go. It typically appears when CGO_ENABLED=0 is set (e.g. for static cross-compiles) on a package that needs cgo.

go build output
# github.com/example/native
./bridge.c:1:10: C source files not allowed when not using cgo: bridge.c

Common causes

cgo disabled for a package that requires it

Setting CGO_ENABLED=0 (common for static or cross-platform builds) turns off cgo, but a dependency’s .c files only make sense with cgo on.

Cross-compiling without a cross C toolchain

Cross-compiles often disable cgo because no cross compiler is available, which then breaks any package carrying C sources.

How to fix it

Enable cgo with a C compiler present

Terminal
CGO_ENABLED=1 go build ./...
# ensure gcc/clang is installed on the runner

Use a pure-Go alternative to drop the C dependency

  1. Identify the package that ships .c files (the error names it).
  2. Replace it with a pure-Go equivalent if one exists, so CGO_ENABLED=0 works.
  3. Keep the dependency only if cgo is genuinely required.

Provide a cross C toolchain for cgo cross-builds

If you must cross-compile a cgo package, supply the matching cross compiler and point cgo at it.

Terminal
CGO_ENABLED=1 CC=aarch64-linux-gnu-gcc GOARCH=arm64 go build ./...

How to prevent it

  • Prefer pure-Go dependencies so CGO_ENABLED=0 builds cleanly.
  • Only set CGO_ENABLED=0 for packages that have no C sources.
  • Provide a cross C toolchain when cgo cross-compilation is required.

Frequently asked questions

What causes ""C source files not allowed""?
Setting CGO_ENABLED=0 (common for static or cross-platform builds) turns off cgo, but a dependency’s .c files only make sense with cgo on.
How do I fix "C source files not allowed"?
Enable cgo with a C compiler present

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card