48

I've installed Go 1.2 on a Windows machine, wrote up a dummy program and set the environment variables GOARCH and GOOS to "AMD64" and "linux" respectively.

When i issue the "go build" command, i receive an error:

go build runtime: linux/amd64 must be bootstrapped using make.bat

What does this mean?

Dante
  • 10,722
  • 16
  • 51
  • 63
  • you can use cywin or WSL go doesnt read all env vars curectly (like arm version) – MSS Feb 06 '23 at 05:14

5 Answers5

57

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

alex
  • 2,178
  • 16
  • 14
  • 3
    I am using go 1.7.5 , seems only `set GOOS=linux` could build linux version . – Mithril Nov 28 '17 at 08:42
  • 1
    What does `go tool dist install -v pkg/runtime` do? – lanoxx Mar 20 '19 at 12:46
  • 2
    @lanoxx that command was required in very old version of Go. If you use go1.12, just set GOARCH and GOOS and run 'go install' or 'go build' command. – alex Mar 21 '19 at 08:44
  • Thanks for the clarification. I have go 1.11.4 installed and received an error when I ran that command but in the end it worked without. – lanoxx Mar 21 '19 at 14:08
40

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.

David O
  • 503
  • 4
  • 6
  • I exactly set `GOARCH=amd64` and `GOOS=linux` by a Run As Administrator PS, alas, when I run `go env`, the result does show anything affected. Windows 10, Go `go1.14.1 windows/amd64`. – M. Rostami Apr 07 '20 at 05:49
  • 3
    Well, as [this answer](https://stackoverflow.com/a/56514992/6602159), I should use another approach when I'm using PS. `$Env:GOOS = "linux"; $Env:GOARCH = "amd64"`. It has worked. Please change your answer and add this important bit. Thank you. – M. Rostami Apr 07 '20 at 05:52
22

To set the PowerShell environment variables use (no admin mode required):
$env:GOOS = "linux"
Than build your programm go build

The changed environment variable is only pesent in the current PowerShell window. Everything will be resetted when you reopen the window.

eDdA
  • 221
  • 2
  • 2
1

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.

From there, here's how it goes if it's like Linux cross-compiling:

First cd to your your go\src directory. If you're not sure where that is, type go env and you'll see a line like GOROOT="\some\dir\" in the output; just do cd \some\dir\src\

Then, with GOOS=linux and GOARCH=amd64 set, type .\make.bat, which will build a version of the Go compiler, etc. targeting Linux. Then you shouldn't get this error anymore.

twotwotwo
  • 28,310
  • 8
  • 69
  • 56
  • It's a batch file (think `AUTOEXEC.bat` from the old days) containing the commands to build Go on your system. – twotwotwo Dec 29 '13 at 20:47
  • It looks like before you can run `make.bat`, you need to download and install the gcc compiler from MinGW. I've added instructions for that. – twotwotwo Dec 29 '13 at 20:56
1

If you using docker, you can build a docker image for linux builts. For example:

First of all, you can prepare a DockerFile as follows:

FROM golang:1.15-alpine3.12 as builder

RUN mkdir /go/src/hello

WORKDIR /go/src/hello

#install nano, zip and git
RUN apk add nano zip git

COPY ./ ./

#build main
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o main

#zip main
RUN zip main.zip main

Than you can build an image with docker build -t yourname/hello . After that have to export your zip file from this image:

  • First, use the image as a container with the command docker run -it yourname / hello.
  • Second, open a new terminal and run the docker ps to view the Container IDs.
  • And finally, run the command docker cp YOUR_WORKING_CONTAINER_ID:/go/src/hello/main.zip . to export the zip file.

This can be a long way, but it will allow you to build using docker without being dependent on the platform. Below is an example developed by me for using a docker image for updating golang lambda function (via aws-cli).

Mehmet Bütün
  • 749
  • 9
  • 19