英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论