Fork me on GitHub

Go build模式

build mode: 用于指导编辑器如何创建可执行的二进制文件。越多的执行方式,就意味着可以让 Go 运行在更多的地方。

1
$go build -buildmode=default goroutine_select.go

在编译go程序时,go编译器支持如下几种模式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
-buildmode=archive
Build the listed non-main packages into .a files. Packages named
main are ignored.

-buildmode=c-archive
Build the listed main package, plus all packages it imports,
into a C archive file. The only callable(可调用的) symbols will be those
functions exported using a cgo //export comment. Requires
exactly one main package to be listed.

-buildmode=c-shared
Build the listed main package, plus all packages it imports,
into a C shared library. The only callable symbols will
be those functions exported using a cgo //export comment.
Requires exactly one main package to be listed.

-buildmode=default
Listed main packages are built into executables and listed
non-main packages are built into .a files (the default
behavior).

-buildmode=shared
Combine all the listed non-main packages into a single shared
library that will be used when building with the -linkshared
option. Packages named main are ignored.

-buildmode=exe
Build the listed main packages and everything they import into
executables. Packages not named main are ignored.

-buildmode=pie
Build the listed main packages and everything they import into
position independent executables (PIE). Packages not named
main are ignored.

-buildmode=plugin
Build the listed main packages, plus all packages that they
import, into a Go plugin. Packages not named main are ignored.

解释:

1
2
3
4
5
6
7
8
exe:        静态编译
exe: 动态链接 libc
exe: 动态链接 libc 和非 Go 代码
pie: 地址无关的可执行文件(安全特性)
c-archive: C 的静态链接库
c-shared: C 的动态链接库
shared: Go 的动态链接库
plugin: Go 的插件
您的鼓励是我持之以恒的动力