英文:
how to create a statically linked golang executable with go 1.5+
问题
golang版本<1.5 - 有很多静态链接的示例、帖子和教程。那么>=1.5呢?(谷歌搜索没有返回我搜索词的有用结果。)有人有关于如何生成一个可以在基本的rkt(来自CoreOS)容器中执行的静态链接二进制文件的建议吗?
我的go版本:
$go version
go version go1.5 linux/amd64
当我尝试运行我的容器时:
sudo rkt --insecure-skip-verify run /tmp/FastBonusReport.aci
我得到:
[38049.477658] FastBonusReport[4]: Error: Unable to open "/lib64/ld-linux-x86-64.so.2": No such file or directory
这表明容器中的可执行文件依赖于这个库,因此不是静态的。
我的清单如下:
cat <<EOF > /tmp/${myapp}/manifest
{
"acKind": "ImageManifest",
"acVersion": "0.9.0",
"name": "${lowermyapp}",
"labels": [
{"name": "os", "value": "linux"},
{"name": "arch", "value": "amd64"}
],
"app": {
"exec": [
"/bin/${myapp}"
],
"user": "0",
"group": "0"
}
}
EOF
我构建二进制文件的命令行如下:
go build ${myapp}.go
这篇文章有一些关于golang<1.5的例子。然后还有这篇入门指南是在CoreOS网站上的。
英文:
golang version < 1.5 - there are plenty of static linking examples, posts and recipes. What about >= 1.5? (google search has returned no useful results for my search terms.) Anyone have any recommendations on how to produce a statically linked binary that can be executed inside a basic rkt (from CoreOS) container?
my go:
$go version
go version go1.5 linux/amd64
when I try to run my container:
sudo rkt --insecure-skip-verify run /tmp/FastBonusReport.aci
I get:
[38049.477658] FastBonusReport[4]: Error: Unable to open "/lib64/ld-linux-x86-64.so.2": No such file or directory
suggesting that the executable in the container is depending on this lib and hence not static.
my manifest looks like:
cat <<EOF > /tmp/${myapp}/manifest
{
"acKind": "ImageManifest",
"acVersion": "0.9.0",
"name": "${lowermyapp}",
"labels": [
{"name": "os", "value": "linux"},
{"name": "arch", "value": "amd64"}
],
"app": {
"exec": [
"/bin/${myapp}"
],
"user": "0",
"group": "0"
}
}
EOF
my command line to build the binary looks like:
go build ${myapp}.go
This article has a few examples golang < 1.5. And then there is this getting started article on the CoreOS site.
答案1
得分: 11
我讨厌回答自己的问题。评论中的CGO_ENABLED=0 go build ./...
似乎解决了问题。
虽然这不是最初的问题的一部分,但一旦程序在rkt容器中开始执行,它就无法执行正确的DNS请求。所以可能还有其他问题。
英文:
I hate to answer my own question. The comments have been correct CGO_ENABLED=0 go build ./...
seems to have have done the trick.
While it was not part of the original question, once the program started executing in the rkt container it could not perform a proper DNS request. So there must be something else going on too.
答案2
得分: 10
静态链接:
Go 1.5:
go build -ldflags "-extldflags -static" ...
在Go 1.6中,我必须使用:
go build -ldflags "-linkmode external -extldflags -static" ...
英文:
Static linking:
Go 1.5:
go build -ldflags "-extldflags -static" ...
With Go 1.6 I had to use:
go build -ldflags "-linkmode external -extldflags -static" ...
答案3
得分: 1
尝试构建静态链接版本:
go build -ldflags '-extldflags "-static"' -tags netgo,osusergo .
使用-tags osusergo,netgo
来强制进行静态构建,无需依赖glibc库。
英文:
try to build static link version :
go build -ldflags '-extldflags "-static"' -tags netgo,osusergo .
use -tags osusergo,netgo
to force static build without glibc dependency library.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论