dpfz27768
2017-04-13 15:37
浏览 467
已采纳

Windows上的golang cgo退出状态2

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
opmind\_obj\
mkdir -p $WORK\_\D_\dev\workspaces\go\src\github.com
opmind\_obj\exe\
cd D:\dev\workspaces\go\src\github.com
opmind

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\
opmind\\_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\
opmind\\_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.

图片转代码服务由CSDN问答提供 功能建议

尝试

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

 包main 
 
 / * 
int theAnswer(){\  n返回42; 
} 
 * / 
import“ C” 
 
import“ fmt” 
 
func main(){
 fmt.Println(C.theAnswer())
} 
   
 
 

这是一个有效的 Idone代码段

问题

ideone 中运行代码段时,按预期工作,但是我自己的计算机上的编译因某种原因失败: \ n

  $ 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 
opmind \ _obj \
mkdir -p $ WORK \ _ \ D_ \ dev \ workspaces \ go \ src \ github.com 
opmind \ _obj \ exe  \
cd D:\ dev \ workspaces \ go \ src \ github.com 
opmind 
 
CGO_LDFLAGS =“-g”“ -O2”“ C:\\ Go \\ pkg \\ tool \\ windows_amd64 \\ cgo  .exe“ 
-obj  dir“ C:\\ Users \\ **** \\ AppData \\ Local \\ Temp \\ go-build775642990 \\ _ \\ D _ \\ dev \\ workspaces \\ go \\ src \\ github.com  \
opmind \\ _ 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 \
opmind \\ _ obj \\“ 
g -O2测试。  go 
 
go build _ / D_ / dev / workspaces / go / src / github.com / nopmind:C:\ Go \ pkg \ tool \ windows_amd64 \ cgo.exe:退出状态2 
   
 
 

$ go build -x 给出相同的输出)

谷歌搜索几个小时后没有任何有用的结果 现在就此刻苦。

环境
  • 操作系统:Windows 8.1 x64
  • 运行:1.8.1
  • MingW: mingw-w64 通过 WinBuilds

    问题

    有人知道为什么会这样吗?

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doujie9882 2017-04-13 18:45
    已采纳

    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.

    打赏 评论

相关推荐 更多相似问题