将多个二进制文件构建到当前目录中。

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

Build multiple binaries into current directory

问题

当在任何目录中执行go build github.com/.../foo时,编译后的二进制文件将放置在我执行该命令的目录中。当执行go build /github.com/.../cmd/...时,我期望所有编译后的二进制文件都出现在当前目录中。但实际上它们没有出现。

我该如何将该包的多个/所有二进制文件编译到当前工作目录中?

英文:

When doing go build github.com/.../foo from any directory, the compiled binary will be placed in the directory from which I executed the command. When doing go build /github.com/.../cmd/... I expected all of the compiled binaries to appear in my current directory. But they don't.

How can I compile multiple/all binaries of that package into my current work dir?

答案1

得分: 1

你可以使用项目文件路径(而不是包名)来编译二进制文件,以便将它们生成到当前目录中。

例如:

$ go build ~/Go/src/github.com/.../cmd

然后二进制文件将位于当前目录中。

英文:

You can compile your binary using the project's file path (instead of the package name) to generate them in your current directory.

For example:

$ go build ~/Go/src/github.com/.../cmd

And the binaries will be in your current directory.

答案2

得分: 1

根据问题23616的陈述:

go build不构建的事实是违反直觉的

当问题[14295 "cmd/go: go build should be able to write multiple executables"] 2得到解决时,您将能够在当前目录中获得多个可执行文件。

但这将在Go 1.13中完成(而不是1.12,所以是在2019年末)。

这离成功非常近。我不确定为什么不行。
我们应该在Go 1.12中修复它。
显然,-o不能与多个二进制目标一起使用,但隐式的“写入当前目录”应该起作用,而实际上它什么都没做。

对于提议的CL有一些讨论,以及CL的实施破坏了go build -a std cmd

请参阅CL 143139/(CL = "Change List")。

英文:

As the issue 23616 states:

> The fact that go build doesn't build is counter-intuitive

You will be able to get multiple binaries in your current directory when... the issue 14295 "cmd/go: go build should be able to write multiple executables" will be completed.

But that will be for Go 1.13 (not 1.12, so late 2019)

> This is so close to working. I'm not sure why it doesn't.
We should fix it for Go 1.12.
Clearly -o can't be used with multiple binary targets, but the implicit "write to the current directory" should work, and instead it does nothing.

> There is a bit of discussion on the proposed CL, as well s the comment that the CL as implemented breaks go build -a std cmd.

See CL 143139/ (CL = "Change List").

答案3

得分: 0

go build -h中:

> 当编译多个包或单个非主包时,build会编译这些包,但会丢弃生成的对象,仅用作检查这些包是否可以构建。

但是你可以使用这个一行命令来实现相同的结果(只会构建直接位于cmd目录下的二进制文件):
find $GOPATH/src/github.com/.../cmd -mindepth 1 -maxdepth 1 | xargs -n1 go build

英文:

From go build -h:

> When compiling multiple packages or a single non-main package,
build compiles the packages but discards the resulting object,
serving only as a check that the packages can be built.

But you can use this one-liner to achieve the same result (only the binaries directly under cmd will be built):
find $GOPATH/src/github.com/.../cmd -mindepth 1 -maxdepth 1 | xargs -n1 go build

huangapple
  • 本文由 发表于 2016年4月11日 18:54:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/36546681.html
匿名

发表评论

匿名网友

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

确定