build modes

The ‘go build’ and ‘go install’ commands take a -buildmode argument which indicates which kind of object file is to be built. Currently supported values are:

 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.

Go 的构建模式

什么是 build mode?

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

Go 的 8 中 build mode

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 的插件

exe 静态编译

exe 动态链接 libc

exe 动态链接 libc 和非 Go 代码

exe 动态链接 libc 和非 Go 代码

pie 地址无关的可执行文件(安全特性)

c-archive C 的静态链接库

c-archive C 的静态链接库

c-shared C 的动态链接库

c-shared C 的动态链接库

shared Go 的动态链接库

shared Go 的动态链接库

See Also

Thanks to the authors 🙂