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

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 在虚拟机中安装flash code
  • ¥15 单片机stm32f10x编写光敏电阻调节3.3伏大功率灯亮度(光强越大灯越暗,白天正常光强灯不亮,使用ADC,PWM等模块)望各位找一下错误或者提供一个可实现功能的代码
  • ¥20 verilog状态机方法流水灯
  • ¥15 pandas代码实现不了意图
  • ¥15 GD32H7 从存储器到外设SPI传输数据无法重复启用DMA
  • ¥25 LT码在高斯信道下的误码率仿真
  • ¥45 渲染完成之后将物体的材质贴图改变,自动化进行这个操作
  • ¥15 yolov5目标检测并显示目标出现的时间或视频帧
  • ¥15 电视版的优酷可以设置电影连续播放吗?