Gin實踐 番外 Golang交叉編譯

項目地址:https://github.com/EDDYCJY/go... (快上車,支持一波)html

原文地址:Golang交叉編譯linux

前言

連載九 講解構建Scratch鏡像時,咱們編譯可執行文件用了另一個形式的命令,不知道你有沒有疑問?git

$ CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o go-gin-example .

說明

咱們將講解命令各個參數的做用,但願你在閱讀時,將每一項串聯起來,你會發現這就是交叉編譯相關的小知識github

也就是 Golang 使人心動的特性之一跨平臺編譯golang

1、CGO_ENABLED

做用:docker

用於標識(聲明) cgo 工具是否可用編程

意義:segmentfault

存在交叉編譯的狀況時,cgo 工具是不可用的。在標準go命令的上下文環境中,交叉編譯意味着程序構建環境的目標計算架構的標識與程序運行環境的目標計算架構的標識不一樣,或者程序構建環境的目標操做系統的標識與程序運行環境的目標操做系統的標識不一樣windows

小結:緩存

結合案例來講,咱們是在宿主機編譯的可執行文件,而在 Scratch 鏡像運行的可執行文件;顯然二者的計算機架構、運行環境標識你沒法肯定它是否一致(畢竟構建的 docker 鏡像還能夠給他人使用),那麼咱們就要進行交叉編譯,而交叉編譯不支持 cgo,所以這裏要禁用掉它

關閉 cgo 後,在構建過程當中會忽略 cgo 並靜態連接全部的依賴庫,而開啓 cgo 後,方式將轉爲動態連接

補充:

golang 是默認開啓 cgo 工具的,可執行 go env 命令查看

$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
...
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
...

2、GOOS

用於標識(聲明)程序構建環境的目標操做系統

如:

  • linux
  • windows

3、GOARCH

用於標識(聲明)程序構建環境的目標計算架構

若不設置,默認值與程序運行環境的目標計算架構一致(案例就是採用的默認值)

如:

  • amd64
  • 386
系統 GOOS GOARCH
Windows 32位 windows 386
Windows 64位 windows amd64
OS X 32位 darwin 386
OS X 64位 darwin amd64
Linux 32位 linux 386
Linux 64位 linux amd64

4、GOHOSTOS

用於標識(聲明)程序運行環境的目標操做系統

5、GOHOSTARCH

用於標識(聲明)程序運行環境的目標計算架構

6、go build

-a

強制從新編譯,簡單來講,就是不利用緩存或已編譯好的部分文件,直接全部包都是最新的代碼從新編譯和關聯

-installsuffix

做用:

在軟件包安裝的目錄中增長後綴標識,以保持輸出與默認版本分開

補充:

若是使用 -race 標識,則後綴就會默認設置爲 -race 標識,用於區別 race 和普通的版本

-o

指定編譯後的可執行文件名稱

小結

大部分參數指令,都有必定關聯性,且與交叉編譯的知識點相關,能夠好好品味一下

最後能夠看看 go build help 加深瞭解

$ go help build
usage: go build [-o output] [-i] [build flags] [packages]
...
    -a
        force rebuilding of packages that are already up-to-date.
    -n
        print the commands but do not run them.
    -p n
        the number of programs, such as build commands or
        test binaries, that can be run in parallel.
        The default is the number of CPUs available.
    -race
        enable data race detection.
        Supported only on linux/amd64, freebsd/amd64, darwin/amd64 and windows/amd64.
    -msan
        enable interoperation with memory sanitizer.
        Supported only on linux/amd64,
        and only with Clang/LLVM as the host C compiler.
    -v
        print the names of packages as they are compiled.
    -work
        print the name of the temporary work directory and
        do not delete it when exiting.
    -x
        print the commands.

    -asmflags '[pattern=]arg list'
        arguments to pass on each go tool asm invocation.
    -buildmode mode
        build mode to use. See 'go help buildmode' for more.
    -compiler name
        name of compiler to use, as in runtime.Compiler (gccgo or gc).
    -gccgoflags '[pattern=]arg list'
        arguments to pass on each gccgo compiler/linker invocation.
    -gcflags '[pattern=]arg list'
        arguments to pass on each go tool compile invocation.
    -installsuffix suffix
        a suffix to use in the name of the package installation directory,
        in order to keep output separate from default builds.
        If using the -race flag, the install suffix is automatically set to race
        or, if set explicitly, has _race appended to it. Likewise for the -msan
        flag. Using a -buildmode option that requires non-default compile flags
        has a similar effect.
    -ldflags '[pattern=]arg list'
        arguments to pass on each go tool link invocation.
    -linkshared
        link against shared libraries previously created with
        -buildmode=shared.
    -pkgdir dir
        install and load all packages from dir instead of the usual locations.
        For example, when building with a non-standard configuration,
        use -pkgdir to keep generated packages in a separate location.
    -tags 'tag list'
        a space-separated list of build tags to consider satisfied during the
        build. For more information about build tags, see the description of
        build constraints in the documentation for the go/build package.
    -toolexec 'cmd args'
        a program to use to invoke toolchain programs like vet and asm.
        For example, instead of running asm, the go command will run
        'cmd args /path/to/asm <arguments for asm>'.
...

參考

本系列示例代碼

本系列目錄

書籍

  • Go併發編程實戰 第二版
相關文章
相關標籤/搜索