1. 程式人生 > >golang 多核編譯

golang 多核編譯

go help build

構建編譯由匯入路徑命名的包,以及它們的依賴關係,但它不會安裝結果.

使用

go build [-o 輸出名] [-i] [編譯標記] [包名]

如果引數為***.go檔案或檔案列表,則編譯為一個個單獨的包。
當編譯單個main包(檔案),則生成可執行檔案。
當編譯單個或多個包非主包時,只構建編譯包,但丟棄生成的物件(.a),僅用作檢查包可以構建。
當編譯包時,會自動忽略'_test.go'的測試檔案。

引數

-o

output 指定編譯輸出的名稱,代替預設的包名。

-i

install 安裝作為目標的依賴關係的包(用於增量編譯提速)。

以下 build 引數可用在 build, clean, get, install, list, run, test

-a
    完全編譯,不理會-i產生的.a檔案(檔案會比不帶-a的編譯出來要大?)
-n
    僅列印輸出build需要的命令,不執行build動作(少用)。
-p n
    開多少核cpu來並行編譯,預設為本機CPU核數(少用)。
-race
    同時檢測資料競爭狀態,只支援 linux/amd64, freebsd/amd64, darwin/amd64 和 windows/amd64.
-msan
    啟用與記憶體消毒器的互操作。僅支援linux / amd64,並且只用Clang / LLVM作為主機C編譯器(少用)。
-v
    打印出被編譯的包名(少用).
-work
    列印臨時工作目錄的名稱,並在退出時不刪除它(少用)。
-x
    同時列印輸出執行的命令名(-n)(少用).
-asmflags 'flag list'
    傳遞每個go工具asm呼叫的引數(少用)
-buildmode mode
    編譯模式(少用)
    'go help buildmode'
-compiler name
    使用的編譯器 == runtime.Compiler
    (gccgo or gc)(少用).
-gccgoflags 'arg list'
    gccgo 編譯/連結器引數(少用)
-gcflags 'arg list'
    垃圾回收引數(少用).
-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 'flag list'
    '-s -w': 壓縮編譯後的體積
    -s: 去掉符號表
    -w: 去掉除錯資訊,不能gdb除錯了
-linkshared
    連結到以前使用建立的共享庫
    -buildmode=shared.
-pkgdir dir
    從指定位置,而不是通常的位置安裝和載入所有軟體包。例如,當使用非標準配置構建時,使用-pkgdir將生成的包保留在單獨的位置。
-tags 'tag list'
    構建出帶tag的版本.
-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 help packages'
對路徑的描述'go help gopath'
對 C/C++ 的互操作'go help c'

注意

構建遵守某些約定('go help gopath'),但不是所有的專案都遵循這些約定,當使用自己的慣例或使用單獨的軟體構建系統時可以選擇使用較低級別的呼叫go tool compilego tool link來避免一些構建工具的開銷和設計決策

See also: go install, go get, go clean.

參考文獻:

https://segmentfault.com/a/1190000008428749