how to create a statically linked golang executable with go 1.5+

huangapple go评论91阅读模式
英文:

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 &quot;/lib64/ld-linux-x86-64.so.2&quot;: No such file or directory

这表明容器中的可执行文件依赖于这个库,因此不是静态的。

我的清单如下:

cat &lt;&lt;EOF &gt; /tmp/${myapp}/manifest
{
    &quot;acKind&quot;: &quot;ImageManifest&quot;,
    &quot;acVersion&quot;: &quot;0.9.0&quot;,
    &quot;name&quot;: &quot;${lowermyapp}&quot;,
    &quot;labels&quot;: [
        {&quot;name&quot;: &quot;os&quot;, &quot;value&quot;: &quot;linux&quot;},
        {&quot;name&quot;: &quot;arch&quot;, &quot;value&quot;: &quot;amd64&quot;}
    ],
    &quot;app&quot;: {
        &quot;exec&quot;: [
            &quot;/bin/${myapp}&quot;
        ],
        &quot;user&quot;: &quot;0&quot;,
        &quot;group&quot;: &quot;0&quot;
    }
}
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 &quot;/lib64/ld-linux-x86-64.so.2&quot;: 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 &lt;&lt;EOF &gt; /tmp/${myapp}/manifest
{
    &quot;acKind&quot;: &quot;ImageManifest&quot;,
    &quot;acVersion&quot;: &quot;0.9.0&quot;,
    &quot;name&quot;: &quot;${lowermyapp}&quot;,
    &quot;labels&quot;: [
        {&quot;name&quot;: &quot;os&quot;, &quot;value&quot;: &quot;linux&quot;},
        {&quot;name&quot;: &quot;arch&quot;, &quot;value&quot;: &quot;amd64&quot;}
    ],
    &quot;app&quot;: {
        &quot;exec&quot;: [
            &quot;/bin/${myapp}&quot;
        ],
        &quot;user&quot;: &quot;0&quot;,
        &quot;group&quot;: &quot;0&quot;
    }
}
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 &quot;-extldflags -static&quot; ...

With Go 1.6 I had to use:

go build -ldflags &quot;-linkmode external -extldflags -static&quot; ...

答案3

得分: 1

尝试构建静态链接版本:

go build -ldflags '-extldflags "-static"' -tags netgo,osusergo .

使用-tags osusergo,netgo来强制进行静态构建,无需依赖glibc库。

英文:

try to build static link version :

go build -ldflags &#39;-extldflags &quot;-static&quot;&#39; -tags netgo,osusergo .

use -tags osusergo,netgo to force static build without glibc dependency library.

huangapple
  • 本文由 发表于 2015年10月14日 05:45:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/33113190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定