Creating a dll with Go 1.7

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

I tried the classic

go build -buildmode=shared main.go 

but get

-buildmode = shared is not supported on windows / amd64

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

+6
source share
2 answers
 go build -buildmode=c-archive github.com/user/ExportHello 

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

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

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

====> will generate Hello2.dll

+14
source

There is a project on github that shows how to create a DLL based on and thanks to user 7155193.

You mainly use GCC to create DLLs from golang, the generated .a and .h files.

First, you create a simple Go file that exports a function (or more).

 package main import "C" import "fmt" //export PrintBye func PrintBye() { fmt.Println("From DLL: Bye!") } func main() { // Need a main function to make CGO compile package as C shared library } 

Compile it with

 go build -buildmode=c-archive exportgo.go 

Then you create the C program (goDLL.c), which will be referenced in the .h and .a files generated above

 #include <stdio.h> #include "exportgo.h" // force gcc to link in go runtime (may be a better solution than this) void dummy() { PrintBye(); } int main() { } 

Compile / Link DLL with GCC:

 gcc -shared -pthread -o goDLL.dll goDLL.c exportgo.a -lWinMM -lntdll -lWS2_32 

Then the goDLL.dll file can be downloaded to another C program, the freepascal / lazarus program, or your program of choice.

The full code with the lazarus / fpc project that loads the DLL is here: https://github.com/z505/goDLL

+7
source

Source: https://habr.com/ru/post/1012279/


All Articles