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?
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.
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 .
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:
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.
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.
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.
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:
Navigate to the folder where the main
package is.
Run $Env:GOOS = "linux"
Optionally run $Env:GOARCH = "amd64"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With