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.
# github.com/example/native
./bridge.c:1:10: C source files not allowed when not using cgo: bridge.cCommon 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
CGO_ENABLED=1 go build ./...
# ensure gcc/clang is installed on the runnerUse a pure-Go alternative to drop the C dependency
- Identify the package that ships
.cfiles (the error names it). - Replace it with a pure-Go equivalent if one exists, so
CGO_ENABLED=0works. - 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.
CGO_ENABLED=1 CC=aarch64-linux-gnu-gcc GOARCH=arm64 go build ./...How to prevent it
- Prefer pure-Go dependencies so
CGO_ENABLED=0builds cleanly. - Only set
CGO_ENABLED=0for packages that have no C sources. - Provide a cross C toolchain when cgo cross-compilation is required.