title: golang cgo exit status 2 on Windows
body: <h3>Attempt</h3>
  
  <p>I want to compile the following snippet on my <code>Windows 8.1</code> machine with <code>Go 1.8.1</code> installed.</p>
  
  <pre><code>package main
  
  /*
  int theAnswer() {
      return 42;
  }
  */
  import "C"
  
  import "fmt"
  
  func main() {
      fmt.Println(C.theAnswer())
  }
  </code></pre>
  
  <p>Here is a working <a href="http://ideone.com/jh6uGP" rel="nofollow noreferrer">Ideone Snippet</a>.</p>
  
  <h3>Problem</h3>
  
  <p>Whilst running the snippet in <code>ideone</code> works as expected, the compilation on my own machine fails somehow:</p>
  
  <pre><code>$ 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
  </code></pre>
  
  <p>(<code>$ go build -x</code> gives same output)</p>
  
  <p>After googling for a few hours without any useful results I'm now hardstuck on this. </p>
  
  <h3>Environment</h3>
  
  <ul>
  <li>OS: Windows 8.1 x64</li>
  <li>Go: 1.8.1</li>
  <li>MingW: <a href="https://mingw-w64.org/doku.php" rel="nofollow noreferrer">mingw-w64</a> installed through <a href="https://mingw-w64.org/doku.php/download/win-builds" rel="nofollow noreferrer">WinBuilds</a></li>
  </ul>
  
  <h3>Question</h3>
  
  <p>Does anyone have a clue why this is happening ?</p>
  
  <p>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.</p>