Golang Cross-compiling

Source: Internet
Author: User
Tags install go
This is a creation in Article, where the information may have evolved or changed.

[TOC]

Description

This article requires more than 1.5 cross-compiling

Golang MAC to compile Windows 64

➜  ~CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o test_win_x64.exe test.go

Linux 64 support under Golang MAC

➜  ~CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o test_linux_x64 test.go

Prepare a cross-compilation environment

Mac or Linux Preparation

Because go after 1.5 has used Golang 1.4来 to compile itself (bootstrap), so need to download golang1.4 first

Https://storage.googleapis.com/golang/go1.4.2.darwin-amd64-osx10.8.tar.gz
Https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz

Setting environment variablesGOROOT_BOOTSTRAP

Unzip to

tar zxvf [go1.4.2.darwin-amd64-osx10.8.tar.gz](https://storage.googleapis.com/golang/go1.4.2.darwin-amd64-osx10.8.tar.gz)cp go/ $home/go-bootstrap/
GOROOT_BOOTSTRAP=$home/go-bootstrap/export GOROOT_BOOTSTRAP

Windows Ready

Download
Http://tdm-gcc.tdragon.net/download
Need to download 32 and 64 install TDM-GCC 32 bit 64 bit and set path

Download
Https://storage.googleapis.com/golang/go1.4.2.windows-amd64.zip
After decompression
Set environment variables GOROOT_BOOTSTRAP to extract directory (prohibit any Chinese, coding problems)

Go to the directory where you want to configure cross-compilation, execute

cd %GOROOT%/srcset CGO_ENABLED=0 | set GOOS=linux | set GOARCH=amd64 | make.bat

Golang Cross-platform cross-compilation

    • Need to enter the $GOROOT/go/src directory where the source code executes
    • Requires a Golang 1.4.x environment
# 如果你想在Windows 32位系统下运行➜  ~cd $GOROOT/src➜  ~CGO_ENABLED=0 GOOS=windows GOARCH=386 ./make.bash# 如果你想在Windows 64位系统下运行➜  ~cd $GOROOT/src➜  ~CGO_ENABLED=0 GOOS=windows GOARCH=amd64 ./make.# 如果你想在OS X 32位系统下运行➜  ~cd $GOROOT/src➜  ~CGO_ENABLED=0 GOOS=darwin GOARCH=386 ./make.bash# 如果你想在OS X 64位系统下运行➜  ~cd $GOROOT/src➜  ~CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 ./make.bash# 如果你想在Linux 32位系统下运行➜  ~cd $GOROOT/src➜  ~CGO_ENABLED=0 GOOS=linux GOARCH=386 ./make.bash# 如果你想在Linux 64位系统下运行➜  ~cd $GOROOT/src➜  ~CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash

You can use cross-compilation only after execution finishes

Not to recompile go, because when you install go, you just compile what the local system needs, and cross-platform cross-compilation, you need to add support for other platforms in go, so there's ./make.bash a process

Error Set $GOROOT _bootstrap

##### Building Go bootstrap tool.cmd/distERROR: Cannot find /Users/xxx/go1.4/bin/go.Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.

Cross-compilation execution

# 如果你想在Windows 32位系统下运行➜  ~CGO_ENABLED=0 GOOS=windows GOARCH=386 go build test.go# 如果你想在Windows 64位系统下运行➜  ~CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build test.go# 如果你想在OS X 32位系统下运行➜  ~CGO_ENABLED=0 GOOS=darwin GOARCH=386 go build test.go# 如果你想在OS X 64位系统下运行➜  ~CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build test.go# 如果你想在Linux 32位系统下运行➜  ~CGO_ENABLED=0 GOOS=linux GOARCH=386 go build test.go# 如果你想在Linux 64位系统下运行➜  ~CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build test.go
    • cgo_enabled = 0 indicates that the settings CGO tool is not available
    • GOOS the target operating system of the program build environment
    • Goarch represents the target computing architecture of the program build environment

Compiling support settings

 goos=windows go build-vgoos=linux go build-vgoos=darwin go build-v  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.