不是“go get”只是获取文件然后使用cgo+“go build”吗?显然不是。

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

Doesn't "go get" just fetch files and then cgo+"go build" them? Apparently not

问题

我在构建一个相当简单的本地CGO项目时遇到了问题。考虑这个非常小的CGO包,它只有3个文件(glut.go,support.c和support.h文件)--如果我只是go get它,这些文件会下载到

C:\Go\src\pkg\github.com\zombiezen\Go-GLUT\glut

然后一些秘密的魔法发生,go get会构建以下输出文件:

C:\Go\pkg\windows_amd64\github.com\zombiezen\Go-GLUT\glut.a

然而,如果我将这三个源文件复制到\mytmp,进入\mytmp并执行go build -o glut.a glut.go,我会得到大约40-60个类似于以下错误消息的错误:

C:\Users\roxor\AppData\Local\Temp\go-build191975110\command-line-arguments\_obj\glut.cgo2.o: In function `_cgo_b46885fb7c57_Cfunc_goCreateMenu':
./glut.go:195: undefined reference to `goCreateMenu'

如果我使用**-x命令行参数运行go get**,我可以看到在构建过程中会捕获到support.c并交给gcc:

gcc -I . -g -O2 -m64 -mthreads -I $WORK\github.com\zombiezen\Go-GLUT\glut\_obj\ -o $WORK\github.com\zombiezen\Go-GLUT\glut\_obj\support.o -c .\support.c

然而,使用go build -x时,情况并非如此,support.c被跳过了。为什么?我该如何调用go build以使其与go get完全相同?

英文:

I have a problem building a fairly simple local CGO project. Consider this very small CGO package of just 3 files (glut.go, support.c and support.h file) -- if I just go get it, these files are downloaded to

C:\Go\src\pkg\github.com\zombiezen\Go-GLUT\glut

then some secret magic happens and go get builds the following output file:

C:\Go\pkg\windows_amd64\github.com\zombiezen\Go-GLUT\glut.a

However, if I copy the three source files to \mytmp, cd into \mytmp and go build -o glut.a glut.go, I get some 40-60 error messages like this one:

C:\Users\roxor\AppData\Local\Temp\go-build191975110\command-line-arguments\_obj\glut.cgo2.o: In function `_cgo_b46885fb7c57_Cfunc_goCreateMenu':
./glut.go:195: undefined reference to `goCreateMenu'

If I run go get with the -x command-line argument, I can see that support.c is picked up during the build process and given to gcc:

gcc -I . -g -O2 -m64 -mthreads -I $WORK\github.com\zombiezen\Go-GLUT\glut\_obj\ -o $WORK\github.com\zombiezen\Go-GLUT\glut\_obj\support.o -c .\support.c

However, with go build -x, this is not the case, support.c is skipped. Why? How do I call go build to work exactly like go get does?

答案1

得分: 0

只需要执行go build而不是go build somefile.go就可以了。现在看起来是如此明显...

英文:

Just doing go build instead of go build somefile.go does it. So obvious now …

huangapple
  • 本文由 发表于 2012年4月30日 09:02:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/10377390.html
匿名

发表评论

匿名网友

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

确定