It looks like your GOOS is getting set to "linux " (with an extra space at the end), not "linux".
Answer from hobbs on Stack Overflowgo - How to cross compile from Windows to Linux? - Stack Overflow
linux - Cross Compiling Go - Stack Overflow
docker - How to create Golang Linux binaries using a Windows host - DevOps Stack Exchange
GOOS=linux GOARCH=amd64 go build go build github.com/ethereum/go-ethereum/crypto/secp256k1: build constraints exclude allGo files in /Users/mac/Documents/project/src/github.com/ethereum/go-ethereum/crypto/secp256k1
It tells you it needs all tools built before you can use them.
If your windows GOARCH is amd64, then you could "build" all required tools by running this small batch programs:
set GOARCH=amd64
set GOOS=linux
go tool dist install -v pkg/runtime
go install -v -a std
If that succeeds then you should be able to do what you've described (just use amd64, not AMD64 - it is case sensitive).
If your windows GOARCH is 386, then you would need to build your 386 tools first. You would need to download mingw gcc for that. Do what user2714852 said.
Here https://golang.org/wiki/WindowsCrossCompiling are similar instructions for linux, perhaps you find them helpful.
Alex
I was having some major problems with building for linux from windows, At the end of the day, it was fairly simple. I would comment on Alex's post, but I can not as I am a stackoverflow newb.
As alex said, set the environment variables. This must be done as administrator (eg right click the "command prompt" or "Powershell" shortcut and click "Run as Administrator")
set GOARCH=amd64
set GOOS=linux
If you use Powershell, use
$Env:GOOS = "linux"; $Env:GOARCH = "amd64"
If you dont do it as administrator, the variables wont take effect and you will just be building it for the OS & Architecture you are on at the moment.
I found its always good to check your go environment vars by running go env, which gives you the list of current go environment variables
go env
set GOARCH=amd64
set GOBIN=
set GOEXE=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=linux
set GOPATH=T:\Projects\gopath
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set GOGCCFLAGS=-fPIC -m64 -fmessage-length=0
set CXX=g++
set CGO_ENABLED=0
set PKG_CONFIG=pkg-config
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
Make sure the GOOS & GOARCH are set to the values you specified earlier.
If thats all good, you should be able to navigate to the directory containing your go code, and from the command line run:
go build
Which will build the package for the system and the architecure that are set in the environment variables.
I encountered some other issues once I finally figured this out, but that is another matter not related to this issue.
What it's saying you need to do is rebuild the library and runtime for linux-amd64. You can do that this way:
- Find the root of your Go installation (if you don't know where this is, running
which gomay help - the binary is often installed with the rest of the sources). cdinto thesrcdirectory- Run
GOOS=linux GOARCH=amd64 ./make.bash --no-clean(orGOOS=linux GOARCH=amd64 bash make.bash --no-cleanifmake.bashis not executable). This will rebuild the library and runtime using the specified OS and architecture.
Once you've done this, you can build a go package or binary for this architecture using GOOS=linux GOARCH=amd64 go build. You can follow the same instructions for other architectures and operating systems.
Edit (08/13/15):
As of Go 1.5, cross compiling is much easier. Since the runtime is written in Go, there's no need to set anything up in order to be able to cross-compile. You can now just run GOOS=<os> GOARCH=<arch> go build from a vanilla Go installation and it will work.
However, there's one exception to this. If you're using cgo, you'll still need to set stuff up ahead of time. And you'll need to inform the tooling that you want to enable cgo cross-compiling by setting the CGO_ENABLED environment variable to 1. So, to be precise:
cdinto thesrcdirectory of your Go installation (see the instructions above).- Run
CGO_ENABLED=1 GOOS=<os> GOARCH=<arch> ./make.bash --no-clean - Run
CGO_ENABLED=1 go buildto build your project. It is important that you specifyCGO_ENABLED=1even when you're compiling.
Following the above answer https://stackoverflow.com/a/27413148/3675575, I needed to set GOROOT_BOOTSTRAP to recompile my GO source tree:
GOROOT_BOOTSTRAP=/usr/lib/golang/ CGO_ENABLED=1 GOOS=linux GOARCH=386 ./make.bash --no-clean
(I'm using Fedora 23, so the GOROOT_BOOTSTRAP may vary in your operating system)
You can easily set the target operating system and processor architecture using the environment variables GOOS and GOARCH respectively. So, as you want to build it for linux operating system, following command with above environment variables will do,
$ GOOS=linux GOARCH=amd64 go build -o hello main.go
Here is the list of all the supported operating system with which you can easily do cross compilation using Go lang.
It seems that docker build opens a new shell and export is needed. The following command created a Linux binary:
export GOOS=linux; go build hello-world.go
Hello,
I have a github actions runner running Linux and it needs time build a go binary using cgo for arm64 architecture. Any examples?
Note that those values are defined in:
src/internal/syslist/syslist.go, anddoc/install/source#environment.
With Go 1.5 (Q3 2015), GOARCH will become much more complete.
See commit 1eebb91 by Minux Ma (minux)
go/build: reserveGOARCHvalues for all common architecturesWhenever we introduce a new
GOARCH, older Go releases won't recognize them and this causes trouble for both our users and us (we need to add unnecessary build tags).Go 1.5 has introduced three new GOARCHes so far:
arm64 ppc64 ppc64le, we can take the time to introduce GOARCHes for all common architectures that Go might support in the future to avoid the problem.
Copyconst goosList = "android darwin dragonfly freebsd linux nacl \
netbsd openbsd plan9 solaris windows "
const goarchList = "386 amd64 amd64p32 arm arm64 ppc64 ppc64le \
mips mipsle mips64 mips64le mips64p32 mips64p32le \ # (new)
ppc s390 s390x sparc sparc64 " # (new)
The list is still being review in Change 9644, with comments like:
I wouldn't bother with Itanium. It's basically a dead architecture.
Plus, it's so hard to write a compiler for it that I really can't see it happening except as a labor of love, and nobody loves the Itanium.
The official documentation now (GO 1.5+ Q3 2015) reflects that completed list.
Update 2018: as documented in Giorgos Oikonomou's answer, Go 1.7 (Q1 2016) has introduced the
go tool dist list command.
See commit c3ecded: it fixes issue 12270 opened in Q3 2015:
To easier write tooling for cross compiling it would be good to programmatically get the possible combinations of GOOS and GOARCH.
This was implemented in CL 19837
cmd/dist: introducelistsubcommand to list all supported platforms
You can list in plain text, or in json:
Copy> go tool dist list -json
[
{
"GOOS": "android",
"GOARCH": "386",
"CgoSupported": true
},
...
]
As Mark Bates tweeted:
Bonus: Column output properly formatted for display:
Copygo tool dist list | column -c 75 | column -t
I think you're looking for this list of possible GOOS and GOARCH combinations, in this section:
http://golang.org/doc/install/source#environment
$GOOS and $GOARCH The name of the target operating system and compilation architecture. These default to the values of $GOHOSTOS and $GOHOSTARCH respectively (described below).
Choices for $GOOS are darwin (Mac OS X 10.8 and above and iOS), dragonfly, freebsd, linux, netbsd, openbsd, plan9, solaris and windows. Choices for $GOARCH are amd64 (64-bit x86, the most mature port), 386 (32-bit x86), arm (32-bit ARM), arm64 (64-bit ARM), ppc64le (PowerPC 64-bit, little-endian), ppc64 (PowerPC 64-bit, big-endian), mips64le (MIPS 64-bit, little-endian), and mips64 (MIPS 64-bit, big-endian). mipsle (MIPS 32-bit, little-endian), and mips (MIPS 32-bit, big-endian).
The valid combinations of $GOOS and $GOARCH are:
Copy$GOOS $GOARCH android arm darwin 386 darwin amd64 darwin arm darwin arm64 dragonfly amd64 freebsd 386 freebsd amd64 freebsd arm linux 386 linux amd64 linux arm linux arm64 linux ppc64 linux ppc64le linux mips linux mipsle linux mips64 linux mips64le netbsd 386 netbsd amd64 netbsd arm openbsd 386 openbsd amd64 openbsd arm plan9 386 plan9 amd64 solaris amd64 windows 386 windows amd64