DEV Community

Haruan Justino
Haruan Justino

Posted on

2

Learning Go Notes - Crosscompiling

As part of my studies with Go, yesterday tried to compile a simple project to windows from my ubuntu linux, and I've got some small tips for that.

1 - To Crosscompile you just need to change 2 enviroment variables, GOARCH (the architecture of the processor) and GOOS (the operational system), you could do this by using export:

export GOARCH="amd64"

export GOOS="windows"

But this makes you need to revert it later.

For a 1 line approach you can pass the parameters before run build command.

GOOS=windows GOARCH=386 go build

2 - If you are using some C library with cgo, you gonna need to enable the CGO_ENABLED flag, and have the structure to compile the C library properly (mingw for windows and things like that). Ex:

GOOS=windows GOARCH=386 CGO_ENABLED=1 CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ go build -ldflags "-H windowsgui" <= this didn't work.

In some cases you can use xgo ( https://github.com/karalabe/xgo ):

xgo --targets=windows/* . would compile to windows.

It was relatively easy to crosscompile, lets check later with larger projects.
If you have some case to tell or some suggestion, please comment.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay