Golang собрать под линукс

How to compile cross-platform Go language project on Linux?

I am trying to set up my Go compiler on Linux which could compile project for any other architecture or platform. I am using default packages from official Ubuntu 14.04 repositories and I am using 64 bit system. This configuration allows me to compile only for Linux and only for 64 bit system. At least I would like to compile for 32 bit Linux or even for 32 bit Windows systems. Is it possible to do somehow? One additional thing is I want to use two Go bindings: https://github.com/mattn/go-gtk and https://github.com/mattn/go-webkit I was testing with go-webkit example code:

package main import ( "os" "github.com/mattn/go-gtk/gtk" "github.com/mattn/go-webkit/webkit" ) const HTML_STRING = `   div   
Hello
世界
` const MAP_EMBED = ` * ` func main() < gtk.Init(nil) window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL) window.SetTitle("webkit") window.Connect("destroy", gtk.MainQuit) vbox := gtk.NewVBox(false, 1) entry := gtk.NewEntry() entry.SetText("http://golang.org/") vbox.PackStart(entry, false, false, 0) swin := gtk.NewScrolledWindow(nil, nil) swin.SetPolicy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) swin.SetShadowType(gtk.SHADOW_IN) webview := webkit.NewWebView() webview.Connect("load-committed", func() < entry.SetText(webview.GetUri()) >) swin.Add(webview) vbox.Add(swin) entry.Connect("activate", func() < webview.LoadUri(entry.GetText()) >) button := gtk.NewButtonWithLabel("load String") button.Clicked(func() < webview.LoadString("hello Go GTK!", "text/plain", "utf-8", ".") >) vbox.PackStart(button, false, false, 0) button = gtk.NewButtonWithLabel("load HTML String") button.Clicked(func() < webview.LoadHtmlString(HTML_STRING, ".") >) vbox.PackStart(button, false, false, 0) button = gtk.NewButtonWithLabel("Google Maps") button.Clicked(func() < webview.LoadHtmlString(MAP_EMBED, ".") >) vbox.PackStart(button, false, false, 0) window.Add(vbox) window.SetSizeRequest(600, 600) window.ShowAll() proxy := os.Getenv("HTTP_PROXY") if len(proxy) > 0 < soup_uri := webkit.SoupUri(proxy) webkit.GetDefaultSession().Set("proxy-uri", soup_uri) soup_uri.Free() >entry.Emit("activate") gtk.Main() >
GOOS=windows GOARCH=386 go build GOARCH=386 go build 

webview.go:5:2: no buildable Go source files in /home/yeeapple/Documents/Coding/Go/Source/src/github.com/mattn/go-gtk/gtk webview.go:6:2: no buildable Go source files in /home/yeeapple/Documents/Coding/Go/Source/src/github.com/mattn/go-webkit/webkit

Another thing I saw is that in GOPATH directory and pkg folder is only «linux_amd64» folder with *.a files. For example, I can compile Go files for other systems if it does not have additional imports. Cross-platform compiling works fine with:

package main import "fmt" func main()
$ go version go version go1.2.1 linux/amd64 $ gccgo --version gccgo (Ubuntu 4.9-20140406-0ubuntu1) 4.9.0 20140405 (experimental) [trunk revision 209157] Copyright (C) 2014 Free Software Foundation, Inc. 

Источник

Читайте также:  Mozilla firefox linux x86

How to cross-compile Go programs for Windows, macOS, and Linux

How do you build Go binaries that target operating systems and architectures other than your own? This is called cross-compiling, and it’s easy to do with Go.

Programs written in Go can easily be compiled for a wide variety of target operating systems such as Windows, macOS, and Linux by using the GOARCH and GOOS environmental variables. These represent the compilation architecture and the name of the target operating system respectively, and are set to your host compilation architecture ( GOHOSTARCH ) and operating system ( GOHOSTOS ) by default.

You can use the go env command to view the values of these variables on your machine:

$ go env . GOARCH="amd64" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" . 

Compile for Windows

Here’s the command you need to run to compile your Go project for a 64-bit Windows machine:

$ GOOS=windows GOARCH=amd64 go build -o bin/app-amd64.exe app.go

In this scenario, GOOS is windows, and GOARCH is amd64 indicating a 64-bit architecture. If you need to support a 32-bit architecture, all you need to do is change GOARCH to 386 .

$ GOOS=windows GOARCH=386 go build -o bin/app-386.exe app.go

Compile for macOS

The GOARCH values for Windows are also valid for macOS, but in this case the required GOOS value is darwin :

# 64-bit $ GOOS=darwin GOARCH=amd64 go build -o bin/app-amd64-darwin app.go  # 32-bit $ GOOS=darwin GOARCH=386 go build -o bin/app-386-darwin app.go

Compile for Linux

To build your Go program for Linux, use linux as the value of GOOS and the appropriate GOARCH value for your target CPU architecture:

# 64-bit $ GOOS=linux GOARCH=amd64 go build -o bin/app-amd64-linux app.go  # 32-bit $ GOOS=linux GOARCH=386 go build -o bin/app-386-linux app.go

Other operating systems and CPU architectures

Go supports a lot more OS and CPU architecture combinations than what I’ve described above, but these are the most used ones. If you need to compile for ARM, Android, Web Assembly, or other targets, please refer to the Go docs to see the combinations of GOOS and GOARCH that are available to you.

Читайте также:  Linux скорость прокрутки тачпада

Thanks for reading, and happy coding!

About the Author

Ayooluwa Isaiah

Ayo is a Software Developer by trade. He enjoys writing about diverse technologies in web development, mainly in Go and JavaScript/TypeScript.
Learn more.

Источник

How to cross compile from Windows to Linux?

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 

5 Answers 5

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.

@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.

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.

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.

Читайте также:  720p to 480p linux

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:

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.

Источник

Оцените статью
Adblock
detector