几天来,我一直在处理与go build的静态链接问题。
复制:
$ git clone https://github.com/IceWhaleTech/CasaOS-UserService.git
$ cd CasaOS-UserService/$ git checkout e2d9ecf2299dca7f65de094e552276286d7bb417$ CGO_ENABLED=1 go build --ldflags '-s -w -extldflags "-static"' -tags musl,netgo ./cmd/migration-tool # Build the code with static linking parameters
$ file migration-tool
migration-tool: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, Go BuildID=6bRf1bPBESbFSmuueVEn/fIR_ucgSolvxHG8CoZsj/AAKVZda1Jflk5MBdCj74/Tv1-5IxySthe8moShu27, stripped输出显示它是动态链接的。
$ git checkout HEAD~1
$ CGO_ENABLED=1 go build --ldflags '-s -w -extldflags "-static"' -tags musl,netgo ./cmd/migration-tool
$ file migration-tool
migration-tool: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, BuildID[sha1]=bddccd9307afc326a4645279c9275b6087acc62a, for GNU/Linux 3.2.0, stripped以前提交的构建是静态链接的!
在这里可以找到两个提交之间的差异。
https://github.com/IceWhaleTech/CasaOS-UserService/commit/e2d9ecf2299dca7f65de094e552276286d7bb417
请大家帮帮忙!
更新-相同的问题发生在另一个项目的更改之后--超级怪异。
https://github.com/IceWhaleTech/CasaOS-Gateway/commit/0fae875b3b1ae6fa0825233e9bb6fcfa6e031381
发布于 2022-11-30 04:02:09
找出原因是因为在当前提交(与以前提交相比)中消除了对go-sqlite3的依赖。出于某种原因,删除将阻止完全相同的命令构建静态链接二进制文件。
尽管我仍然对为什么感到好奇,但解决方法是使用不含CGO的sqlite实现(如https://pkg.go.dev/modernc.org/sqlite ),然后在不使用CGO_ENABLED=1的情况下重建构建命令,并将osusergo添加到tags中。
$ go build --ldflags '-s -w -extldflags "-static"' -tags musl,netgo,osusergo ./cmd/migration-toolhttps://stackoverflow.com/questions/74621343
复制相似问题