Analysis of cross-platform cross-compilation of Golang

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

What is cross-platform cross-compilation

Cross-compiling

In layman's words, you compile a program on a platform that can run on several other platforms (often referred to as the difference between a system and CPU architecture)

Cross-compilation is commonly used when distributing binaries that are available on multiple platforms, such as compiling EXE programs that can be used under win under Linux.

Local compilation

Local compilation refers to the current system configuration compiler compiled according to the current system configuration of the current system is applicable to the execution program (some other languages in the local compilation may be due to the extension of the problem, can not be run on the same platform other machines).

So if you want to build programs on other platforms and systems that are not native, you need to use cross-compilation (cross-compilation toolchain).

Cross-compilation tool chain

The > Cross-compilation toolchain is a comprehensive development environment consisting of compilers, connectors, and interpreters, and the cross-compilation toolchain consists of 3 parts, binutils, GCC, and glibc.

> Sometimes in order to reduce the size of the LIBC library, other C libraries can be used instead of glibc, such as UCLIBC, DIETLIBC, and Newlib.

Cross-platform cross-compilation of Golang

The go language is a compiled language that allows programs to be compiled and run in other operating systems, only to add support for other systems at compile time.

Cross-compiling relies on the following environment variables

    • Goarch processor architecture for Target platform (compiled target platform) (386, AMD64, ARM)
    • Operating system for GOOS target platform (compiled target platform) (Darwin, FreeBSD, Linux, Windows)

GOOS and Goarch support for each platform

GOOS Goarch OS version
Linux 386/amd64/arm >= Linux 2.6
Darwin 386/amd64 OS X (Snow Leopard + Lion)
Freebsd 386/amd64 >= FreeBSD 7
Windows 386/amd64 >= Windows 2000

Golang cross-compilation steps (can be skipped)

> First enter the $GOROOT/GO/SRC source directory, execute the following command to create the target platform required package and tool files

# 如果你想在Windows 32位系统下运行cd $GOROOT/srcCGO_ENABLED=0 GOOS=windows GOARCH=386 ./make.bash# 如果你想在Windows 64位系统下运行cd $GOROOT/srcCGO_ENABLED=0 GOOS=windows GOARCH=amd64 ./make.bash# 如果你想在Linux 32位系统下运行cd $GOROOT/srcCGO_ENABLED=0 GOOS=linux GOARCH=386 ./make.bash# 如果你想在Linux 64位系统下运行cd $GOROOT/srcCGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash

Cross-compiling the current project

# 如果你想在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# 如果你想在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

The cgo_enabled = 0 in the command above indicates that the settings CGO tool is not available, GOOS represents the target operating system (Linux, Windows) for the program build environment, and Goarch represents the target computing architecture (32-bit, 64-bit) of the program-building environment;

Now you can run the compiled program on the relevant target operating system.

Link

Cross compilation of cross-platform cross-compilation on Go

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.