golang cgo exit status 2 on Windows

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

golang cgo exit status 2 on Windows

问题

尝试

我想在安装了Go 1.8.1的Windows 8.1机器上编译以下代码片段。

package main

/*
int theAnswer() {
    return 42;
}
*/
import "C"

import "fmt"

func main() {
    fmt.Println(C.theAnswer())
}

这里是一个可工作的 Ideone Snippet

问题

ideone上运行代码片段时,结果如预期。但是在我的机器上编译失败了:

$ CC=gcc GOARCH=amd64 GOOS=windows go build -x
WORK=C:\Users\****\AppData\Local\Temp\go-build775642990
mkdir -p $WORK\_\D_\dev\workspaces\go\src\github.com\nopmind\_obj\
mkdir -p $WORK\_\D_\dev\workspaces\go\src\github.com\nopmind\_obj\exe\
cd D:\dev\workspaces\go\src\github.com\nopmind

CGO_LDFLAGS="-g" "-O2" "C:\\Go\\pkg\\tool\\windows_amd64\\cgo.exe" 
-objdir "C:\\Users\\****\\AppData\\Local\\Temp\\go-build775642990\\_\\D_\\dev\\workspaces\\go\\src\\github.com\\nopmind\\_obj\\" 
-importpath _/D_/dev/workspaces/go/src/github.com/nopmind 
-- -I "C:\\Users\\****\\AppData\\Local\\Temp\\go-build775642990\\_\\D_\\dev\\workspaces\\go\\src\\github.com\\nopmind\\_obj\\" 
-g -O2 Test.go

go build _/D_/dev/workspaces/go/src/github.com/nopmind: C:\Go\pkg\tool\windows_amd64\cgo.exe: exit status 2

$ go build -x 输出相同)

在没有任何有用结果的情况下,我已经谷歌了几个小时,现在陷入了困境。

环境

  • 操作系统:Windows 8.1 x64
  • Go版本:1.8.1
  • MingW:通过WinBuilds安装的mingw-w64

问题

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

我个人猜测是MingW安装有问题。如果是这样,我希望能得到一个简短的解释,如何正确设置MingW以支持cgo。

英文:

Attempt

I want to compile the following snippet on my Windows 8.1 machine with Go 1.8.1 installed.

package main

/*
int theAnswer() {
    return 42;
}
*/
import "C"

import "fmt"

func main() {
    fmt.Println(C.theAnswer())
}

Here is a working Ideone Snippet.

Problem

Whilst running the snippet in ideone works as expected, the compilation on my own machine fails somehow:

$ CC=gcc GOARCH=amd64 GOOS=windows go build -x
WORK=C:\Users\****\AppData\Local\Temp\go-build775642990
mkdir -p $WORK\_\D_\dev\workspaces\go\src\github.com\nopmind\_obj\
mkdir -p $WORK\_\D_\dev\workspaces\go\src\github.com\nopmind\_obj\exe\
cd D:\dev\workspaces\go\src\github.com\nopmind

CGO_LDFLAGS="-g" "-O2" "C:\\Go\\pkg\\tool\\windows_amd64\\cgo.exe" 
-objdir "C:\\Users\\****\\AppData\\Local\\Temp\\go-build775642990\\_\\D_\\dev\\workspaces\\go\\src\\github.com\\nopmind\\_obj\\" 
-importpath _/D_/dev/workspaces/go/src/github.com/nopmind 
-- -I "C:\\Users\\****\\AppData\\Local\\Temp\\go-build775642990\\_\\D_\\dev\\workspaces\\go\\src\\github.com\\nopmind\\_obj\\" 
-g -O2 Test.go

go build _/D_/dev/workspaces/go/src/github.com/nopmind: C:\Go\pkg\tool\windows_amd64\cgo.exe: exit status 2

($ go build -x gives same output)

After googling for a few hours without any useful results I'm now hardstuck on this.

Environment

Question

Does anyone have a clue why this is happening ?

My personal guess is that something is wrong with the MingW installation. If so I'd appreciate a short explanation how to properly set MingW up for cgo.

答案1

得分: 4

你的代码适用于Windows 10、Windows 7和Linux。例如,在Windows 10上,可以使用TDM-GCC来进行MinGW编译。

Microsoft Windows [版本 10.0.15063]

>go version
go version devel +dc0f0ab Thu Apr 13 18:20:38 2017 +0000 windows/amd64

>gcc --version
gcc (tdm64-1) 5.1.0

>type answer.go
package main

/*
int theAnswer() {
    return 42;
}
*/
import "C"

import "fmt"

func main() {
        fmt.Println(C.theAnswer())
}

>go run answer.go
42

>

我从未使用过Win-builds

英文:

Your code works on Windows 10 and 7 and Linux. For example, on Windows 10, using TDM-GCC for MinGW,

Microsoft Windows [Version 10.0.15063]

>go version
go version devel +dc0f0ab Thu Apr 13 18:20:38 2017 +0000 windows/amd64

>gcc --version
gcc (tdm64-1) 5.1.0

>type answer.go
package main

/*
int theAnswer() {
    return 42;
}
*/
import "C"

import "fmt"

func main() {
        fmt.Println(C.theAnswer())
}

>go run answer.go
42

>

I have never used Win-builds.

huangapple
  • 本文由 发表于 2017年4月13日 23:37:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/43396453.html
匿名

发表评论

匿名网友

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

确定