Sub topic 12
When cgo and native libraries complicate cross-builds
This chapter assumes you have read the earlier chapters. You should know how FROM --platform=$BUILDPLATFORM pins a stage to the build machine. You should also know how TARGETOS and TARGETARCH feed GOOS and GOARCH. The Go examples so far set CGO_ENABLED=0. This chapter covers what changes when you can't do that.
Why cgo breaks the simple recipe
Go cross-compiles easily because the Go compiler is itself a cross-compiler. Setting GOOS and GOARCH is enough. That only holds for pure Go code.
cgo lets Go call C code. When it is enabled, go build runs a C compiler and links against C libraries. Those must be built for the target architecture. Setting GOARCH=arm64 on an amd64 machine then fails, because the default gcc produces amd64 objects.
You can hit cgo without noticing:
- A dependency such as
github.com/mattn/go-sqlite3wraps a C library. - The standard
netandos/userpackages use cgo by default when it is available. - Some image, crypto, and database drivers link against system libraries.
Typical symptoms are gcc: error: unrecognized command-line option messages, file in wrong format linker errors, or a binary that builds but fails at runtime.
Your options
flowchart TDThe diagram below is the decision path to follow.
- Avoid cgo. Replace the dependency with a pure-Go one, for example
modernc.org/sqliteinstead ofgo-sqlite3. This keeps the fast path and a static binary. - Install a cross C compiler in the build stage and point cgo at it with
CC. The stage still runs natively. - Run the stage under emulation. Drop
--platform=$BUILDPLATFORMso QEMU runs the whole stage as the target. This is the simplest option and the slowest.
A cross toolchain for cgo
This build stage runs natively and installs Debian's cross compilers. It picks one by TARGETARCH. The code is plain C-backed cgo with no extra libraries.
# syntax=docker/dockerfile:1
FROM --platform=$BUILDPLATFORM golang:1.22-bookworm AS build
ARG TARGETOS
ARG TARGETARCH
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc-aarch64-linux-gnu libc6-dev-arm64-cross \
gcc-x86-64-linux-gnu libc6-dev-amd64-cross \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN case "$TARGETARCH" in \
amd64) CC=x86_64-linux-gnu-gcc ;; \
arm64) CC=aarch64-linux-gnu-gcc ;; \
*) echo "unsupported arch: $TARGETARCH" >&2; exit 1 ;; \
esac; \
CGO_ENABLED=1 CC="$CC" GOOS=$TARGETOS GOARCH=$TARGETARCH \
go build -o /out/app ./cmd/app
FROM gcr.io/distroless/base-debian12
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]Two details matter here.
- The final stage uses
distroless/base-debian12, notdistroless/static. A cgo binary is dynamically linked against glibc by default, andstaticcontains no libc. Running the binary there fails with a confusing "no such file or directory" error, even though/appexists. The missing file is the dynamic loader. - The
casestatement fails loudly for architectures you haven't handled. This is safer than silently using the native compiler.
Libraries beyond libc
The recipe above covers C code that needs only libc. If your program links against something like libsqlite3 or libpng, you also need that library's headers and shared objects built for the target architecture. Your options are:
- Enable multi-arch packages with
dpkg --add-architecture arm64and installlibfoo-dev:arm64. Version conflicts between architectures are common. - Compile the library from source for the target in the build stage.
- Use a helper project such as
tonistiigi/xx, which wraps these steps. - Fall back to emulation for that stage.
Exercise
Your cross-built cgo image builds for linux/arm64 but crashes at startup with "no such file or directory" for /app, which clearly exists. The final stage is gcr.io/distroless/static-debian12. What is the likely cause and fix?
Show answer
The binary is dynamically linked against glibc, and the static image has no libc or dynamic loader. Either switch the final stage to an image with glibc, such as distroless/base-debian12, or make the binary static by avoiding cgo or linking statically.