DEV Community


Use CGO in Windows 64bit

sofiawinters profile image Sofia Winters ・2 min read

To use CGO in Windows 64bit, you have to install gcc. The easiest way to get gcc which is eligible for CGO is to install MinGW-w64.

The official documentation also refers mingw-w64 as a gcc compiler for CGO.

In order to use cgo on Windows, you'll also need to first install a gcc compiler (for instance, mingw-w64) and have gcc.exe (etc.) in your PATH environment variable before compiling with cgo will work.

The CGO page of the go official wiki

Download MINGW-W64

Download a MinGW-W64 installer from MinGW-W64-builds.

Install MINGW-W64

Execute the downloaded installer, then change Architecture to x86_64 and Threads to win32.

Add path to mingw64/bin to PATH

Add the path to mingw64/bin to your system PATH environment variable. The path may look like C:\Program Files\mingw-w64\x86_64******\mingw64\bin.

There you go!

This is a tiny go library source.

// main.go
package main

import "fmt"
import "C"

// no space before export!
//export hello
func hello() {

// you have to define the main function
func main() {
Enter fullscreen mode Exit fullscreen mode

To build:

go build -buildmode=c-shared -o hello.dll
Enter fullscreen mode Exit fullscreen mode

To export a header file:

go tool cgo main.go
Enter fullscreen mode Exit fullscreen mode

It generates c source and header files into the _obj directory.

If you use Visual Studio, you can inspect the dll by dumpbin.exe.

dumpbin /exports hello.dll | grep hello
Enter fullscreen mode Exit fullscreen mode

It'll be like this:

> dumpbin.exe /exports hello.dll | grep hello
Dump of file hello.dll
  Section contains the following exports for hello.dll
         15    E 000A7760 _cgoexp_7cdc2f248ed0_hello
         96   5F 000A77C0 hello
        302  12D 000A76C0 main._cgoexpwrap_7cdc2f248ed0_hello
Enter fullscreen mode Exit fullscreen mode

You can call the function from C#:

using System.Runtime.InteropServices;

namespace Main
    class Program
        [DllImport("hello.dll", EntryPoint = "hello")]
        static extern void Hello();

        static void Main(string[] args)
            Hello(); // => Hello!
Enter fullscreen mode Exit fullscreen mode


Editor guide