如何在1.1.2中编译Go程序,并使用在1.1.1中编译的依赖项?

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

How to compile go program in 1.1.2 with dependencies compiled in 1.1.1?

问题

每次我尝试在升级了Go从1.1.1到1.1.2之后(在Windows 7 64位系统上)编译我的程序时,我都会收到如下的错误信息:

C:\Users\VonC\prog\go\src\github.com\spf13\hugo>go build -o hugo.exe main.go
# github.com/spf13/hugo/hugolib
hugolib\page.go:23: import C:\Users\VonC\prog\go\pkg\windows_amd64/github.com/emicklei/hopwatch.a: 
  object is [windows amd64 go1.1.1 X:none] 
  expected [windows amd64 go1.1.2 X:none]

我尝试了go clean -r,但错误信息仍然存在。

正确的clean命令是什么?

英文:

Every time I try to compile my program after (this morning) upgrading go from 1.1.1 to 1.1.2 (on Windows 7 64 bits), I get error message like:

C:\Users\VonC\prog\go\src\github.com\spf13\hugo>go build -o hugo.exe main.go
# github.com/spf13/hugo/hugolib
hugolib\page.go:23: import C:\Users\VonC\prog\go\pkg\windows_amd64/github.com/emicklei/hopwatch.a: 
  object is [windows amd64 go1.1.1 X:none] 
  expected [windows amd64 go1.1.2 X:none]

I tried a go clean -r, but the error message persists?

What is the right clean command to use?

答案1

得分: 4

实际解决方案:

我使用以下方法重现了这个问题:

  • 直接调用1.1.2版本的go.exe(我的路径中没有%GOROOT%\bin
  • GOROOT指向之前安装的1.1.1版本的文件夹(我将go 1.1.1和1.1.2安装在不同的文件夹中)。

如果你使用的是默认的go设置(即:一个 C:\go安装目录,并且%GOROOT%\bin在你的PATH中),你不会看到这个错误。

但是如果你确实看到了这个错误:

  • 确保%GOROOT%与你调用的go.exe一致
  • 按照下面的说明执行go install -a。下面提到的go clean不是必需的。
    正如jnml评论中所说:

> Go构建系统应该能够找出$GOPATH/pkg中的任何过时内容,并在需要时(传递地)重新构建它。


原始解决方案:

在“Command go”页面的“删除对象文件”部分,我忽略了go clean -i选项:

-i

> -i标志会导致clean删除相应的已安装的存档文件或二进制文件(即go install会创建的文件)。

而那些.a文件(如hopwatch.a)正是go install为库生成的文件(在Windows中)。

因此,完整的clean命令,以确保go重新构建所有内容,应该是:

cd C:\Users\VonC\prog\go\src\github.com\spf13\hugo
go clean -r -i
go install -a

这将重新构建和安装所有内容,包括所有依赖包。

-a实际上是一个构建选项,它强制重新构建已经是最新版本的包。


通常情况下,go clean -r -n显示将要被清理的内容(-n:预览选项)。
在实际删除任何内容之前,确保知道将要被删除的内容是没有坏处的。

英文:

Actual solution:

I reproduced the issue with:

  • calling the 1.1.2 go.exe directly (I didn't have %GOROOT%\bin in my path)
  • with GOROOT pointing to the previous 1.1.1 installation folder (I kept go 1.1.1 and 1.1.2 installed in separated folders).

If you are sticking with the default go setup (ie: one C:\go installation directory, and %GOROOT%\bin in your PATH), you won't see this error.

But if you do see this error:

  • make sure %GOROOT% is consistent with the go.exe you are calling
  • go install -a as explained below. The go clean mentioned below won't be necessary.
    As jnml comments:

> the Go build system is supposed to figure out any obsolete stuff in $GOPATH/pkg and (transitively) rebuild it on demand.


Original solution:

In the "Remove object files" section of "Command go" page, I missed the go clean -i option:

-i

> The -i flag causes clean to remove the corresponding installed archive or binary (what 'go install' would create).

And those .a file (like hopwatch.a) are precisely what go install generates for libraries (in Windows).

So the full clean command, to make sure go rebuild everything, would be:

cd C:\Users\VonC\prog\go\src\github.com\spf13\hugo
go clean -r -i
go install -a

That will rebuild and install everything, including all dependent packages.

The -a is actually a build option, which forces rebuilding of packages that are already up-to-date.


As usual, go clean -r -n would show you what would be cleaned (-n: preview option).
It doesn't hurt to be sure of what will be deleted... before actually deleting anything.

huangapple
  • 本文由 发表于 2013年8月13日 16:47:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/18204545.html
匿名

发表评论

匿名网友

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

确定