How Do I Cross-Compile Go Binaries For S390x?

2025-09-03 10:17:32
292
Share
ABO Personality Quiz
Take a quick quiz to find out whether you‘re Alpha, Beta, or Omega.
Start Test
Write Answer
Ask Question

3 Answers

Kieran
Kieran
Favorite read: Sphinx
Active Reader Driver
Quick tips I keep pinned: pure-Go cross-compiles are frictionless — GOOS=linux GOARCH=s390x CGO_ENABLED=0 go build — and you can inspect the result with file to verify architecture. If you get an ELF header or illegal instruction at runtime, double-check GOARCH, re-run file, and ensure you didn’t accidentally copy an amd64 binary into an s390x host.

If you must use cgo, the headache is the cross toolchain: you need an s390x-targeting gcc and the right sysroot; set CC and CGO_ENABLED=1 and be ready for platform-specific build flags. For CI, either spin up an s390x runner or use qemu with binfmt_misc so your runners can execute s390x binaries. And remember little conveniences: add -ldflags='-s -w' to strip debug symbols, use module-aware builds (GOFLAGS or go env GOPROXY) so dependency resolution is stable, and test on real hardware when possible — emulators are great, but the hardware occasionally surprises me.
2025-09-06 00:05:59
9
Nolan
Nolan
Favorite read: GO ROGUE
Honest Reviewer Mechanic
Building Go for s390x is way easier than I used to expect — once you know the tiny set of knobs to flip. I’ve cross-compiled a couple of small services for IBM Z boxes and the trickiest part was simply remembering to disable cgo unless I had a proper cross-GCC toolchain.

Practically, for a pure Go program the canonical command I use is very simple: set GOOS=linux and GOARCH=s390x, and turn off CGO so the build doesn’t try to invoke a C compiler. For example:

GOOS=linux GOARCH=s390x CGO_ENABLED=0 go build -o myprog_s390x ./...

That produces an s390x ELF binary you can check with file myprog_s390x. If you need smaller binaries I usually add ldflags like -ldflags='-s -w'. If your project uses cgo (native libs), you’ll need a cross-compiler for s390x and to set CC appropriately (e.g. CC=s390x-linux-gnu-gcc), but the package names and toolchain installation vary by distro. When I couldn’t access hardware I tested with qemu (qemu-system-s390x for full systems, or register qemu-user in binfmt_misc) to sanity-check startup.

I also sometimes use Docker buildx or CI (GitHub Actions) to cross-build images, but for pure Go binaries the env-variable approach is the fastest way to get a working s390x binary on an x86 machine. If you run into weird syscalls or platform-specific bugs, running the binary on a real s390x VM or CI runner usually tells you what to fix.
2025-09-07 00:24:45
20
Zane
Zane
Novel Fan Nurse
Cutting to the chase: I like a compact, no-nonsense checklist when I need reproducible cross-builds. Start by deciding whether you need cgo. If not, the job is trivial: environment variables, go build, confirm with file.

Commands I run most often:
GOOS=linux GOARCH=s390x CGO_ENABLED=0 go build -v -o ./build/myapp_s390x ./cmd/myapp
file ./build/myapp_s390x # should report ELF 64-bit LSB executable, s390x

If your app links to native libs (cgo), you’ll either install an s390x cross-toolchain on your build machine or build on an s390x builder. On Debian/Ubuntu the cross gcc often appears under names like s390x-linux-gnu-gcc (package names vary). Then set CC to that cross-compiler and keep CGO_ENABLED=1. Example:
CGO_ENABLED=1 CC=s390x-linux-gnu-gcc GOOS=linux GOARCH=s390x go build -o myprog_s390x

For CI or multi-arch Docker images I use Docker Buildx or GitHub Actions with a build matrix (GOARCH: s390x). Buildx can produce images for linux/s390x directly (docker buildx build --platform linux/s390x …), which is handy when you want the whole image to run on IBM Z hardware or emulators. When in doubt test on a real s390x VM or via qemu-system-s390x; that catches runtime behavior differences fast.
2025-09-09 09:41:21
9
View All Answers
Scan code to download App

Related Books

Related Searches

Explore and read good novels for free
Free access to a vast number of good novels on GoodNovel app. Download the books you like and read anywhere & anytime.
Read books for free on the app
SCAN CODE TO READ ON APP
DMCA.com Protection Status