How to cross compile from Windows to Linux?

后端 未结 4 1289
[愿得一人]
[愿得一人] 2020-12-01 00:42

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\"

相关标签:
4条回答
  • 2020-12-01 01:16

    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.

    0 讨论(0)
  • 2020-12-01 01:24

    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.

    0 讨论(0)
  • 2020-12-01 01:37

    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.

    0 讨论(0)
  • 2020-12-01 01:40

    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

    0 讨论(0)
提交回复
热议问题