Go build在忽略文件时没有给出任何提示。

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

Go build ignores files without saying anything

问题

我在尝试构建我的Go程序的Windows版本时遇到了一些非常奇怪的行为。

我的目录包含:

foo.go
foo_windows.go
foo_windows_test.go
foo_unix.go
foo_linux.go
foo_linux_test.go
foo_darwin.go

构建Windows版本失败,因为构建过程忽略了foo_windows.go文件,原因不明。请注意,任何文件中都没有// +build的注释。以下是构建文件列表的输出:

$ GOOS=linux GOARCH=amd64 go list -f '{{.IgnoredGoFiles}}' github.com/foo/
[foo_darwin.go foo_windows.go foo_windows_test.go]

$ GOOS=darwin GOARCH=amd64 go list -f '{{.IgnoredGoFiles}}' github.com/foo/
[foo_linux.go foo_linux_test.go foo_windows.go foo_windows_test.go]

$ GOOS=windows GOARCH=amd64 go list -f '{{.IgnoredGoFiles}}' github.com/foo/
[foo_darwin.go foo_linux.go foo_linux_test.go foo_unix.go foo_windows.go]

显然,最后一项是非常错误的。foo_windows.go不应该被忽略。请注意,当将foo_windows.go中的方法直接包含在foo.go中时,Windows版本构建成功,因此它确实排除了该文件,而该文件包含了构建所需的所有内容。

在Mac上进行交叉编译和在Windows机器上正常编译都会导致文件被排除的相同问题,这让我相信问题可能出在文件本身上,但我不确定可能是什么原因。

有人知道为什么会发生这种情况吗?

英文:

I'm getting some really strange behavior when attempting to build a windows version of my go program.

My directory contains:

foo.go  
foo_windows.go  
foo_windows_test.go  
foo_unix.go  
foo_linux.go  
foo_linux_test.go  
foo_darwin.go  

Windows builds are failing because the build ignores foo_windows.go, for some reason. Note, there are no // +build comments in any files. Here's the output of the build file list:

$ GOOS=linux GOARCH=amd64 go list -f '{{.IgnoredGoFiles}}' github.com/foo/
[foo_darwin.go foo_windows.go foo_windows_test.go]

$ GOOS=darwin GOARCH=amd64 go list -f '{{.IgnoredGoFiles}}' github.com/foo/
[foo_linux.go foo_linux_test.go foo_windows.go foo_windows_test.go]

$ GOOS=windows GOARCH=amd64 go list -f '{{.IgnoredGoFiles}}' github.com/foo/
[foo_darwin.go foo_linux.go foo_linux_test.go foo_unix.go foo_windows.go]

Obviously the last item is very wrong. foo_windows.go should not be ignored. Note that when including the methods in foo_windows.go in foo.go directly, the windows build succeeds, so it really is excluding the file, and the file contains everything needed for a good build.

Both cross-compiling on a mac and compiling normally on a windows machine lead to the same problems of file exclusion, which leads me to believe it's something about the file itself that is causing problems, but I'm not sure what it might be.

Does anyone have any idea why this is happening?

答案1

得分: 0

我将回答自己的问题,并希望能帮助其他人。

foo_windows.go没有被包含在构建中,因为存在未表达的构建错误。具体来说,foo_windows.go包含C代码,由于默认情况下CGO_ENABLED=0,go没有尝试编译它,它只是看到文件中有C代码并排除了该文件。将CGO_ENABLED=1设置为构建中包含该文件。

英文:

I will answer my own question and hope it helps someone else.

foo_windows.go was not included in the build because of unexpressed build errors. Specifically, foo_windows.go contains C code, and because CGO_ENABLED=0 by default, go didn't try compiling it, it just saw there was C code in the file and excluded the file. Setting CGO_ENABLED=1 included the file in the build.

huangapple
  • 本文由 发表于 2015年11月11日 00:05:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/33634358.html
匿名

发表评论

匿名网友

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

确定