首页 > 解决方案 > 使用 cgo 库的应用程序的静态 golang 构建

问题描述

我决定使用 zstd 库在我的代码中使用来自 datadog 的 Go 包装器进行压缩
我的应用程序构建命令是(我在 gitlab-ci 中使用 构建我的应用程序image: golang:1.10.2-alpine

dep ensure --vendor-only
CGO_ENABLE=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-extldflags "-static"' cmd/main.go

哪个失败了

build constraints exclude all Go files in /go/src/xxxxx/xxxx/xxxx/vendor/github.com/DataDog/zstd

当我尝试启用CGO_ENABLED(并保持其余构建命令不变)时,我最终收到警告:

# command-line-arguments
/tmp/go-link-395607750/000054.o: In function `mygetgrouplist':
/usr/local/go/src/os/user/getgrouplist_unix.go:15: warning: Using 'getgrouplist' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/tmp/go-link-395607750/000053.o: In function `mygetgrgid_r':
/usr/local/go/src/os/user/cgo_lookup_unix.go:38: warning: Using 'getgrgid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/tmp/go-link-395607750/000053.o: In function `mygetgrnam_r':
/usr/local/go/src/os/user/cgo_lookup_unix.go:43: warning: Using 'getgrnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/tmp/go-link-395607750/000053.o: In function `mygetpwnam_r':
/usr/local/go/src/os/user/cgo_lookup_unix.go:33: warning: Using 'getpwnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/tmp/go-link-395607750/000053.o: In function `mygetpwuid_r':
/usr/local/go/src/os/user/cgo_lookup_unix.go:28: warning: Using 'getpwuid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/tmp/go-link-395607750/000004.o: In function `_cgo_f7895c2c5a3a_C2func_getaddrinfo':
/tmp/go-build/cgo-gcc-prolog:46: warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

这似乎不太好,因为我想让我的构建静态链接。我是否必须首先构建 zstd 库然后构建我的应用程序?

标签: gobuildstatic-libraries

解决方案


推荐阅读