golang uses dynamic link libraries (shared link libraries) for compilation

Using go help build mode, we can see that go can be built in many ways, using static link libraries by default.

➜  src go help buildmode
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 buildmode

We use shared mode on macos, but display does not support it. We switch to linux platform for experiment:

➜  src go install -buildmode=shared yxpkg 
-buildmode=shared not supported on darwin/amd64

Create libstd.so libraries:

root@docker ~/go# go install -buildmode=shared std

Create the so library for the yxpkg package:

root@docker ~/go# go install -buildmode=shared -linkshared yxpkg

Compile main.go to generate dynamically linked executable files:

root@docker ~/g/src# go build -linkshared yaoxu.go

We compare the executable files generated before with static links: we find that the size of the executable files varies greatly;

root@docker ~/g/src# ll
total 1.9M
-rwxr-xr-x. 1 root root  22K Aug 29 17:17 yaoxu*
-rw-r--r--. 1 root root   87 Aug 29 16:57 yaoxu.go
drwxr-xr-x. 2 root root 4.0K Aug 29 16:27 yxpkg/
-rwxr-xr-x. 1 root root 1.9M Aug 29 16:57 yx_static*

We use ldd to view two files separately:

 

As can be seen, two files are dynamic link file and static link file.

It should be noted that when go compiles dynamic links, it still needs source code files to assist compilation. I think the main reason is to build symbol tables.

There are also some specific details, you can configure your own environment, self-test;

The directory structure of the compiled workspace is as follows:

 

Among them, yxpkg is a package, and the function content in yxpkg package is used in yaoxu.go file.

Workspace code can be found in the following connection: https://github.com/yaowenxu/Workplace/tree/master/go

Keep updated. If it's helpful to you, please pay attention to cnblogs.com/xuyaowen

Tags: Go Docker Linux github

Posted on Tue, 08 Oct 2019 05:10:21 -0700 by Dennis Madsen