GO using a dynamic link library (shared libraries) to compile a dynamically linked executable file

We used go help buildmode can see the go can be constructed in a variety of ways, using the default static link library.

➜  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

On macos we use shared mode, but the display is not supported, we replaced the linux platform to experiment:

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

Creating libstd.so library:

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

Create a library so yxpkg package:

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

Compile main.go generated dynamically linked executable:

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

We compared the previously generated statically linked executable: find its executable file size, vary 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 are using ldd see two files:

 

 Visible, the two documents is a dynamic link files, a file is statically linked.

Which should be noted that, go be linked dynamically compile time, or need assistance to compile source code files, I think the main reason is to build a symbol table.

There are some specific details, you can configure their own environment, to test themselves;

The directory structure of the compiled work area is as follows:

 

 Wherein, yxpkg a package, yaoxu.go file content used to function yxpkg packet;

Work area code can be found in the following connections: https://github.com/yaowenxu/Workplace/tree/master/go

Stay updated, if helpful to you, please pay attention cnblogs.com/xuyaowen

 

Guess you like

Origin www.cnblogs.com/xuyaowen/p/go-build-dynamic-lib.html