作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢!
我的 golang 项目中使用了 gozstd, 在 macbook m2 下编译当然没有问题。
可是当交叉编译 linux amd64 的二进制时,遇到了问题。
使用 gnu-gcc 是 ok 的
env CC=x86_64-linux-gnu-gcc CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CGO_LDFLAGS="-static" \
go build -o cmd/server_linux_amd cmd/main.go
使用 musl-gcc 报错
env CC=x86_64-linux-musl-gcc CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CGO_LDFLAGS="-static" \
go build -o cmd/server_linux_amd cmd/main.go
出现类似这样的错误:
/opt/homebrew/Cellar/go/1.20.4/libexec/pkg/tool/darwin_arm64/link: running x86_64-linux-musl-gcc failed: exit status 1
/opt/homebrew/Cellar/musl-cross/0.9.9_1/libexec/bin/../lib/gcc/x86_64-linux-musl/9.2.0/../../../../x86_64-linux-musl/bin/ld: /Users/fuchunzhang/go/pkg/mod/github.com/valyala/[email protected]/libzstd_linux_amd64.a(zdict.o): in function `ZDICT_analyzeEntropy':
zdict.c:(.text+0x7d4): undefined reference to `__fprintf_chk'
/opt/homebrew/Cellar/musl-cross/0.9.9_1/libexec/bin/../lib/gcc/x86_64-linux-musl/9.2.0/../../../../x86_64-linux-musl/bin/ld: zdict.c:(.text+0xd21): undefined reference to `__fprintf_chk'
/opt/homebrew/Cellar/musl-cross/0.9.9_1/libexec/bin/../lib/gcc/x86_64-linux-musl/9.2.0/../../../../x86_64-linux-musl/bin/ld: /Users/fuchunzhang/go/pkg/mod/github.com/valyala/[email protected]/libzstd_linux_amd64.a(zdict.o): in function `ZDICT_analyzePos':
zdict.c:(.text+0x1922): undefined reference to `__fprintf_chk'
/opt/homebrew/Cellar/musl-cross/0.9.9_1/libexec/bin/../lib/gcc/x86_64-linux-musl/9.2.0/../../../../x86_64-linux-musl/bin/ld: zdict.c:(.text+0x19b2): undefined reference to `__fprintf_chk'
/opt/homebrew/Cellar/musl-cross/0.9.9_1/libexec/bin/../lib/gcc/x86_64-linux-musl/9.2.0/../../../../x86_64-linux-musl/bin/ld: /Users/fuchunzhang/go/pkg/mod/github.com/valyala/[email protected]/libzstd_linux_amd64.a(zdict.o): in function `ZDICT_finalizeDictionary':
zdict.c:(.text+0x1cf3): undefined reference to `__fprintf_chk'
/opt/homebrew/Cellar/musl-cross/0.9.9_1/libexec/bin/../lib/gcc/x86_64-linux-musl/9.2.0/../../../../x86_64-linux-musl/bin/ld: /Users/fuchunzhang/go/pkg/mod/github.com/valyala/[email protected]/libzstd_linux_amd64.a(zdict.o):zdict.c:(.text+0x20b5): more undefined references to `__fprintf_chk' follow
collect2: error: ld returned 1 exit status
解决办法
最后搜索到作者的 github,帖子里介绍了解决办法:https://github.com/valyala/gozstd/issues/20
最终用这样的命令行解决了:
env CC=x86_64-linux-musl-gcc CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CGO_LDFLAGS="-static" \
go build -tags 'musl' -o cmd/server_linux_amd cmd/main.go
标签:gcc,x86,..,musl,gozstd,64,linux
From: https://www.cnblogs.com/ahfuzhang/p/17470156.html