生成Windows DLL

You need mingw-64. I just was able to link an amd64 DLL using that.

Yes just add .dll or add the flag -o name.dll
Yes this is a x64 binary, you need to crosscompile to x86 if you want a x86 binary.

I can’t help you much on what to read, just use google and ask around in the proper place
https://github.com/golang/go/wiki/Questions

  1. 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

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

====> will generate Hello2.dll

There is a project on github which shows how to create a DLL, based on, and thanks to user7155193’s answer.

Basically you use GCC to build the DLL from golang generated .a and .h files.

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

  1. package main
  2. import "C"
  3. import "fmt"
  4. //export PrintBye
  5. func PrintBye() {
  6. fmt.Println("From DLL: Bye!")
  7. }
  8. func main() {
  9. // Need a main function to make CGO compile package as C shared library
  10. }

Compile it with:

  1. go build -buildmode=c-archive exportgo.go

Then you make a C program (goDLL.c) which will link in the .h and .a files generated above

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

Compile/link the DLL with GCC:

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

The goDLL.dll then can be loaded into another C program, a freepascal/lazarus program, or your program of choice.

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

ft_authoradmin  ft_create_time2018-07-17 01:37
 ft_update_time2018-07-17 01:41