摘要:交叉編譯項(xiàng)目地址快上車,支持一波原文地址前言在連載九講解構(gòu)建鏡像時(shí),我們編譯可執(zhí)行文件用了另外一個(gè)形式的命令,不知道你有沒有疑問說明我們將講解命令各個(gè)參數(shù)的作用,希望你在閱讀時(shí),將每一項(xiàng)串聯(lián)起來,你會(huì)發(fā)現(xiàn)這就是交叉編譯相關(guān)的小知識(shí)
Golang交叉編譯
項(xiàng)目地址:https://github.com/EDDYCJY/go... (快上車,支持一波)
原文地址:https://segmentfault.com/a/11...
前言在 連載九 講解構(gòu)建Scratch鏡像時(shí),我們編譯可執(zhí)行文件用了另外一個(gè)形式的命令,不知道你有沒有疑問?
$ CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o go-gin-example .說明
我們將講解命令各個(gè)參數(shù)的作用,希望你在閱讀時(shí),將每一項(xiàng)串聯(lián)起來,你會(huì)發(fā)現(xiàn)這就是交叉編譯相關(guān)的小知識(shí)
也就是 Golang 令人心動(dòng)的特性之一跨平臺(tái)編譯
一、CGO_ENABLED作用:
用于標(biāo)識(shí)(聲明) cgo 工具是否可用
意義:
存在交叉編譯的情況時(shí),cgo 工具是不可用的。在標(biāo)準(zhǔn)go命令的上下文環(huán)境中,交叉編譯意味著程序構(gòu)建環(huán)境的目標(biāo)計(jì)算架構(gòu)的標(biāo)識(shí)與程序運(yùn)行環(huán)境的目標(biāo)計(jì)算架構(gòu)的標(biāo)識(shí)不同,或者程序構(gòu)建環(huán)境的目標(biāo)操作系統(tǒng)的標(biāo)識(shí)與程序運(yùn)行環(huán)境的目標(biāo)操作系統(tǒng)的標(biāo)識(shí)不同
小結(jié):
結(jié)合案例來說,我們是在宿主機(jī)編譯的可執(zhí)行文件,而在 Scratch 鏡像運(yùn)行的可執(zhí)行文件;顯然兩者的計(jì)算機(jī)架構(gòu)、運(yùn)行環(huán)境標(biāo)識(shí)你無法確定它是否一致(畢竟構(gòu)建的 docker 鏡像還可以給他人使用),那么我們就要進(jìn)行交叉編譯,而交叉編譯不支持 cgo,因此這里要禁用掉它
關(guān)閉 cgo 后,在構(gòu)建過程中會(huì)忽略 cgo 并靜態(tài)鏈接所有的依賴庫(kù),而開啟 cgo 后,方式將轉(zhuǎn)為動(dòng)態(tài)鏈接
補(bǔ)充:
golang 是默認(rèn)開啟 cgo 工具的,可執(zhí)行 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" ...二、GOOS
用于標(biāo)識(shí)(聲明)程序構(gòu)建環(huán)境的目標(biāo)操作系統(tǒng)
如:
linux
windows
三、GOARCH用于標(biāo)識(shí)(聲明)程序構(gòu)建環(huán)境的目標(biāo)計(jì)算架構(gòu)
若不設(shè)置,默認(rèn)值與程序運(yùn)行環(huán)境的目標(biāo)計(jì)算架構(gòu)一致(案例就是采用的默認(rèn)值)
如:
amd64
386
系統(tǒng) | 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 |
用于標(biāo)識(shí)(聲明)程序運(yùn)行環(huán)境的目標(biāo)操作系統(tǒng)
五、GOHOSTARCH用于標(biāo)識(shí)(聲明)程序運(yùn)行環(huán)境的目標(biāo)計(jì)算架構(gòu)
六、go build -a強(qiáng)制重新編譯,簡(jiǎn)單來說,就是不利用緩存或已編譯好的部分文件,直接所有包都是最新的代碼重新編譯和關(guān)聯(lián)
-installsuffix作用:
在軟件包安裝的目錄中增加后綴標(biāo)識(shí),以保持輸出與默認(rèn)版本分開
補(bǔ)充:
如果使用 -race 標(biāo)識(shí),則后綴就會(huì)默認(rèn)設(shè)置為 -race 標(biāo)識(shí),用于區(qū)別 race 和普通的版本
-o指定編譯后的可執(zhí)行文件名稱
小結(jié)大部分參數(shù)指令,都有一定關(guān)聯(lián)性,且與交叉編譯的知識(shí)點(diǎn)相關(guān),可以好好品味一下
最后可以看看 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參考 本系列示例代碼". ...
go-gin-example
本系列目錄連載一 Golang介紹與環(huán)境安裝
連載二 搭建Blog API"s(一)
連載三 搭建Blog API"s(二)
連載四 搭建Blog API"s(三)
連載五 使用JWT進(jìn)行身份校驗(yàn)
連載六 編寫一個(gè)簡(jiǎn)單的文件日志
連載七 Golang優(yōu)雅重啟HTTP服務(wù)
連載八 為它加上Swagger
連載九 將Golang應(yīng)用部署到Docker
連載十 定制 GORM Callbacks
連載十一 Cron定時(shí)任務(wù)
連載十二 優(yōu)化配置結(jié)構(gòu)及實(shí)現(xiàn)圖片上傳
連載十三 優(yōu)化你的應(yīng)用結(jié)構(gòu)和實(shí)現(xiàn)Redis緩存
連載十四 實(shí)現(xiàn)導(dǎo)出、導(dǎo)入 Excel
連載十五 生成二維碼、合并海報(bào)
連載十六 在圖片上繪制文字
連載十七 用 Nginx 部署 Go 應(yīng)用
番外 Golang交叉編譯
番外 請(qǐng)入門 Makefile
書籍Go并發(fā)編程實(shí)戰(zhàn) 第二版
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/28463.html
摘要:交叉編譯項(xiàng)目地址快上車,支持一波原文地址前言在連載九講解構(gòu)建鏡像時(shí),我們編譯可執(zhí)行文件用了另外一個(gè)形式的命令,不知道你有沒有疑問說明我們將講解命令各個(gè)參數(shù)的作用,希望你在閱讀時(shí),將每一項(xiàng)串聯(lián)起來,你會(huì)發(fā)現(xiàn)這就是交叉編譯相關(guān)的小知識(shí) Golang交叉編譯 項(xiàng)目地址:https://github.com/EDDYCJY/go... (快上車,支持一波) 原文地址:https://segme...
摘要:也可以是某個(gè)操作的名字標(biāo)簽,稱為偽目標(biāo)前置條件,這一項(xiàng)是可選參數(shù)。明明只是執(zhí)行命令,為什么會(huì)打印到標(biāo)準(zhǔn)輸出上了原因默認(rèn)會(huì)打印每條命令,再執(zhí)行。 Golang Gin實(shí)踐 番外 請(qǐng)入門 Makefile 原文地址:Golang Gin實(shí)踐 番外 請(qǐng)入門 Makefile 前言 含一定復(fù)雜度的軟件工程,基本上都是先編譯 A,再依賴 B,再編譯 C...,最后才執(zhí)行構(gòu)建 如果每次都人為編排,...
摘要:將應(yīng)用部署到項(xiàng)目地址快上車,支持一波原文地址注開始前你需要安裝好,配好鏡像源本章節(jié)源碼在分支上從本章節(jié)開始項(xiàng)目目錄都以為基準(zhǔn)請(qǐng)配合自己本地項(xiàng)目靈活變動(dòng)介紹在這里簡(jiǎn)單介紹下,建議深入學(xué)習(xí)是一個(gè)開源的輕量級(jí)容器技術(shù),讓開發(fā)者可以打包他們 將Golang應(yīng)用部署到Docker 項(xiàng)目地址:https://github.com/EDDYCJY/go... (快上車,支持一波)原文地址:https...
閱讀 1288·2021-11-15 11:37
閱讀 2285·2021-09-30 09:55
閱讀 4613·2021-09-22 15:51
閱讀 3791·2021-09-22 15:46
閱讀 2795·2019-08-30 15:52
閱讀 469·2019-08-29 16:20
閱讀 2923·2019-08-29 15:12
閱讀 1192·2019-08-26 18:27