安装一个Go二进制文件,保留依赖项/包。

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

Install a Go binary, keep dependencies / packages

问题

当我构建一个Go二进制文件时,通常会像这样做:

go build -ldflags "-X main.basedir somevar" -o mybuilddir/bin/myfile mypackage/main

这将构建二进制文件并将其放置在自定义目录中。但是这样做不会保留pkg/目录下的“中间”包文件,这会加快下一次编译的速度。

解决方案是使用go install命令,但是我无法指定输出目录。似乎可以使用GOBIN来设置二进制文件目录,但我无法指定可执行文件的名称(始终为main)。

有什么可能的解决方案吗?

  • 自定义目标目录
  • 自定义名称(不是main
  • 保留生成的中间包文件(.a

这是GOPATHsrc目录结构:

GOPATH/src$ tree 
.
└── mypackage
    ├── packagea
    │   └── packagea.go
    ├── packageb
    │   └── packageb.go
    └── main
        └── mypackage.go

使用go install命令,包文件(.a)会被创建在$GOPATH/pkg目录下,而使用go build命令,我无法在任何地方找到.a文件。

英文:

When I build a Go binary, I usually do something like this:

go build -ldflags "-X main.basedir somevar" -o mybuilddir/bin/myfile mypackage/main"

this builds the binary and places it in a custom directory. But this doesn't keep the "intermediate" package files beneath pkg/, which would speed up the next compilation runs.

The solution would be go install, but I cannot specify an output directory. It seems to be possible to set the binary directory with GOBIN, but I am unable to specify the name of the executable (always main).

What is a possible solution to this problem?

  • Custom destionation directory
  • Custom name (not main)
  • Keep intermediate generated package files (.a)

This is the src directory of GOPATH:

GOPATH/src$ tree 
.
└── mypackage
    ├── packagea
    │   └── packagea.go
    ├── packageb
    │   └── packageb.go
    └── main
        └── mypackage.go

With go install, the package files (.a) are created in $GOPATH/pkg, with go build, I can't find the .a files anywhere.

答案1

得分: 6

更新于2017年11月:Go 1.10(2018年第一季度)将为go build和go install添加缓存功能:参见“go build不必要地重新构建”。


原始答案(2014年)

> 使用go install命令,包文件(.a)将被创建在$GOPATH/pkg目录下,但是使用go build命令,我找不到.a文件的位置。

正如在“go build命令是如何工作的?”中提到的:

> 最后一步是将目标文件打包成一个存档文件.a,链接器和编译器会使用这个文件。

> 因为我们在一个包上调用了go build命令,所以构建完成后,结果会被丢弃,因为$WORK目录会被删除。如果我们使用go install -x命令,输出中会出现两行额外的信息:

mkdir -p /home/dfc/go/pkg/linux_arm/crypto/
cp $WORK/crypto/hmac.a /home/dfc/go/pkg/linux_arm/crypto/hmac.a

> 这展示了go buildinstall之间的区别;

> - build只构建,

  • install构建并安装结果供其他构建使用。

英文:

Update Nov. 2017: Go 1.10 (Q1 2018) will add caching for go build and go install: see "go build rebuilds unnecessarily".


Original answer (2014)

> With go install, the package files (.a) are created in $GOPATH/pkg, with go build, I can't find the .a files anywhere.

As mentioned in "How does the go build command work ?"

> The final step is to pack the object file into an archive file, .a, which the linker and the compiler consume.

> Because we invoked go build on a package, the result is discarded as $WORK is deleted after the build completes.
If we invoke go install -x two additional lines appear in the output

mkdir -p /home/dfc/go/pkg/linux_arm/crypto/
cp $WORK/crypto/hmac.a /home/dfc/go/pkg/linux_arm/crypto/hmac.a

> This demonstrates the difference between go build and install;

> - build builds,

  • install builds then installs the result to be used by other builds.

huangapple
  • 本文由 发表于 2014年11月18日 16:39:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/26989909.html
匿名

发表评论

匿名网友

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

确定