go cross-compiler language

problem

How to file golang another platform that can be performed in a compiler platform. For example, to compile files on mac and linux Windows can perform. So our problem is set to: how to compile 64-bit linux on a mac executable file.

solution

To ensure that the cross compiler golang golang version 1.5 or more, the present solution execute the example code version 1.9. We want to compile a file of hello.go hello.go

package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n")
}

On the mac compile command to compile 64-bit linux command bash:

GOOS=linux GOARCH=amd64 go build hello.go

The code above which run directly on the command console 64 can generate a linux executable. Parameter analysis here uses two variables:

GOOS: target operating system GOARCH: the target operating system architecture

OS|ARCH|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

Compile time compile other platforms according to the table above parameters on it. Further reading on the web many tutorial may see the following compilation command

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build hello.go

Wherein CGO_ENABLED = 0 means that the use of the C language version of compiler GO parameter settings when closed to 0 C language version of the compiler. Since golang1.5 go to go use the language compiler to compile. None of the golang1.9 use CGO_ENABLED parameters found can still compile. Of course, you can also use a normal compilation. For example, the CGO_ENABLED parameter is set to 1, that the use of CGO in the process of compiling a compiler, I can still find a normal compilation. In fact if you use the C library go in them, such as the use of go build import "C" when it will start CGO default compiler, of course, we can use CGO_ENABLED = 0 to control whether to use the CGO go build a compiler.

Guess you like

Origin www.cnblogs.com/double12gzh/p/11294767.html