Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 463
Stef Avatar asked Nov 13 '16 11:11

Stef


People also ask

Can Golang create DLL?

Short answer: yes. Long answer: yes, but there are a couple of things to be aware of, both on the Go side and the C side. A short introduction to creating shared C libraries in Go can be found here and a longer one here. (Found by searching Google for go dynamic library .)

How do I manually install a DLL in Windows 10?

Go to the Search window and type cmd, right click on Command Prompt and choose Run as an administrator option. Type the following command in the command window and hit Enter - regsvr32 <dllname>


2 Answers

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.

like image 68
Andrew Dolder Avatar answered Sep 28 '22 18:09

Andrew Dolder


go build -buildmode=c-archive github.com/user/ExportHello

====> will build ExportHello.a, ExportHello.h

Take the functions built in ExportHello.a and re-export in Hello2.c

gcc -shared -pthread -o Hello2.dll Hello2.c ExportHello.a -lWinMM -lntdll -lWS2_32

====> will generate Hello2.dll

like image 39
user7155193 Avatar answered Sep 28 '22 16:09

user7155193