在alpine镜像基础上安装的go1.12的容器中编译go项目报错

mkdir -p release/1.0.3
GOOS=linux GOARCH=amd64 go build -o release/1.0.3/myProject-1.0.3-linux-amd64

/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/…/…/…/…/x86_64-alpine-linux-musl/bin/ld: cannot find Scrt1.o: No such file or directory
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/…/…/…/…/x86_64-alpine-linux-musl/bin/ld: cannot find crti.o: No such file or directory
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/…/…/…/…/x86_64-alpine-linux-musl/bin/ld: cannot find -lpthread
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/…/…/…/…/x86_64-alpine-linux-musl/bin/ld: cannot find -ldl
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/…/…/…/…/x86_64-alpine-linux-musl/bin/ld: cannot find -lssp_nonshared
collect2: error: ld returned 1 exit status
make: *** [Makefile:34: linux] Error 2

解决

编译时设置CGO_ENABLE=0, 强制关闭cgo(允许你在Go代码中调用C代码),默认开启.

CGO_ENABLED=0 go build

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐