Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to CrossCompile Go programs on Windows 10

I want to compile a Go programm for a linux machine. I always used that way which is described here:

How to cross compile from Windows to Linux?

That worked pretty well until the last big update from Windows 10. Now I am not able to set the GOOS with

set GOOS=linux

I also tried to start the PowerShell as administrator, but even that is not working. Are there any tools which can do that? Or is there another way to crosscompile Go program on Windows 10?

like image 473
apxp Avatar asked Jun 18 '18 13:06

apxp


People also ask

Are Go programs cross platform?

The cross-platform language can be used with various platforms like UNIX, Linux, Windows, and other operating systems that work on mobile devices as well. Go made it in the headlines two years back (2017) when it managed to beat Python, C, as well as Java to win the programming language of the year title.

How do you compile Go for Linux in Windows?

To cross-compile Go, fist you need to be able to build Go from the source code. To do that, it looks like you need to install MinGW to get gcc and other tools. Help on that is at https://code.google.com/p/go-wiki/wiki/WindowsBuild. Then, with GOOS=linux and GOARCH=amd64 set, type .

How to cross compile CGO on Mac and Linux?

You can’t use CGO when cross compiling. Make sure CGO_ENABLED is set to 0 Now it is time to test if you can build Go programs for both the Mac and Linux operating systems. Set up a quick GOPATH and Go program. In Terminal run the following commands:

Do I need to cross compile my Go code?

If you don’t have the need to cross compile your code then I recommend you stick with the traditional distribution packages and installs for Go. If this is something you need, then it all starts with downloading the current release of the Go source code. The Go source code is stored in a DVCS called Github and is located on github.com/golang/go.

Do I need the make tool to compile in go?

Often the release page would have windows executable but no instructions on how to compile on Windows, only on mac/linux with make There are no more Makefiles needed in Go, so the make tool isn't necessary. You also do not need cygwin.

Is it possible to cross-compile go with C code in Arch Linux?

I recently needed to test some app on windows, and while cross-compiling in Go is straight-forward, the app had some C code and apparently on Arch Linux the mingw64-gcc package is misconfigured. After some head scratching I figured out a workaround until the package is fixed (task #4313), you have to link your Go code against ssp.


1 Answers

set is an internal command of the Windows command line interpreter (cmd.exe).

If you're using PowerShell, then changing values of environment variables should be done like:

$Env:<variable-name> = "<new-value>"

For more details, see PowerShell documentation: About Environment Variables

So to change GOOS, use:

$Env:GOOS = "linux"

To do a cross-compilation:

  1. Navigate to the folder where the main package is.

  2. Run $Env:GOOS = "linux"

  3. Optionally run $Env:GOARCH = "amd64"

  4. Run go build

Or you can do it in a single line:

$Env:GOOS = "linux"; $Env:GOARCH = "amd64"; go build

To specify the output file name:

$Env:GOOS = "linux"; $Env:GOARCH = "amd64"; go build -o hello
like image 85
icza Avatar answered Oct 27 '22 19:10

icza