DEV Community

Haruan Justino
Haruan Justino

Posted on

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.

Top comments (0)