go 1.21 GOOS=linux build failed
bowin opened this issue · comments
Bo Hu commented
Bo Hu commented
cross compile from mac to linux, it seems like cgo problem
OldPanda commented
@bowin You're right. CGO is the reason why it failed to cross compile your go program. As far as I know, there're two workarounds to compile the go code to the target platform.
- Using Docker. See details at https://www.docker.com/blog/faster-multi-platform-builds-dockerfile-cross-compilation-guide/
- Using Zig. I tried
GOOS=linux GOARCH=amd64 CGO_ENABLED=1 CC="zig cc -target x86_64-linux-musl" CXX="zig c++ -target x86_64-linux-musl" go build
on my side and it didn't complain anything. However, I don't have a Linux machine to try run the binary. You can checkout the details at https://blog.afoolishmanifesto.com/posts/golang-zig-cross-compilation/ and give it a shot.