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

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.

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题