8

Golang | cmd - go build 构建模式

 2 years ago
source link: https://ijayer.github.io/post/tech/code/golang/20180624-go_cmd_01_2_go-build-mode/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Golang | cmd - go build 构建模式

2018-06-24 533 words 2 mins read 89 times read

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:

-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

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 🙂

Author zher

LastMod 2018-06-24

License CC BY-NC-ND 4.0

Golang | cmd - go build 构建 C 的静态 + 动态链接库、Go 的动态链接库 Golang | cmd - go build 构建约束

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK