DEV Community

Cover image for Zig Makes Go Cross Compilation Just Work

Zig Makes Go Cross Compilation Just Work

Loris Cro on January 24, 2021

See also the followup post: https://zig.news/kristoff/building-sqlite-with-cgo-for-every-os-4cic For the last couple of months I worked on a redes...
Collapse
 
fufuu profile image
fufuu • Edited

I run this command to cross compile from window to linux but it doesn't work.
go version go1.17 windows/amd64
$ env CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC=zig cc -target x86_64-linuxCXX=zig c++ -target x86_64-linux` go build

output :
zig: error: no input files
zig: error: no input files

Collapse
 
kristoff profile image
Loris Cro • Edited

Turns out the required patch did not land in Go 1.17 so you will have to use the workaround still, sorry! I've updated the article.

Collapse
 
gogo profile image
Gobro

Does this support cross compiling from linux to darwin? I can't make it work.
I followed your script workaround, except I swapped -target x86_64-linux with x86_64-macos.

I run:
GOARCH=amd64 CGO_ENABLED=1 GOOS=darwin CC="zcc" CXX="zxx" go build -buildmod
e=c-shared -o test.dll

And I get the error:
/usr/local/go/pkg/tool/linux_amd64/link: running zcc failed: exit status 1
warning: unsupported linker arg: -headerpad
warning: unsupported linker arg: 1144
warning: unsupported linker arg: --compress-debug-sections=zlib-gnu
Cannot open /tmp/go-link-029541769/go.o: bad relocation (no atom found for defined symbol) in section TEXT/text (r_address=5024, r_type=1, r_extern=1, r_length=2, r_pcrel=1, r_symbolnum=620)
error: FileNotFound

Collapse
 
kristoff profile image
Loris Cro

Generally speaking, yes, Zig can also crosscompile for macOS. You might have encountered a bug in Zig or maybe it's a problem related to the dll file you're providing (IIRC ARM M1 macs need position independent code, for example).

I recommend trying with a recent build of Zig (you can get nightlies from the official website) and if it's still broken there, then open an issue on GitHub.

Collapse
 
dosgo profile image
dosgo

I developed a small tool to automatically judge the compilation target based on the GOOS GOARCH environment.
github.com/dosgo/zigtool

Collapse
 
wobsoriano profile image
Robert • Edited

Does this work on M1?

Collapse
 
nektro profile image
Meghan (she/her)

Woah! The more I learn about what Zig is capable of, the more it continues to amaze me.

Collapse
 
fr3fou profile image
fr3fou

this is quite nice!

Collapse
 
amarting profile image
Alvaro Martin

Hi, I am running all the commands above.
Process:
brew install zig
added zip dir to path
building docker image (linux/amd64) of go1.18.2-alpine3.14 (so same as the example, with go 1.18 or above) on a macbook m1 machine.

RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC="zig cc -target x86_64-linux" CXX="zig c++ -target x86_64-linux" \
go build --tags extended -ldflags="-w -s" -o app.go

Getting this error:

runtime/cgo

cgo: C compiler "zig" not found: exec: "zig": executable file not found in $PATH

Anyone knows what might be wrong? Any direction?
Thanks!

Collapse
 
fahmifan profile image
fahmi irfan

This is nice

Collapse
 
booniepepper profile image
J.R. Hill

For folks wondering about Rust cross-compilation, see also the fantastic cargo-zigbuild

Collapse
 
karel_bilek_61506cb0df404 profile image
Karel Bilek

Now I just wish there was a way to do the macos notarization nonsense without macos access…