Search code examples
windowsgodllmingw-w64

Building a dll with Go 1.7


Is there a way to build a dll against Go v1.7 under Windows ?

I tried a classic

go build -buildmode=shared main.go

but get

-buildmode=shared not supported on windows/amd64

update Ok, I've got my answer. For those who are interested : https://groups.google.com/forum/#!topic/golang-dev/ckFZAZbnjzU


Solution

  • As of Go 1.10, -buildmode=c-shared is now supported on Windows.

    Release notes: https://golang.org/doc/go1.10#compiler

    So now compiling to DLL is a one-liner:

    go build -o helloworld.dll -buildmode=c-shared
    

    I believe the headers are only compatible with GCC. If you're only exposing C-types, this should not be a big issue. I was able to get LoadLibrary to work in Visual Studio without the header.