This is a creation in Article, where the information may have evolved or changed.
The go toolchain currently [does not support the compilation of dynamic link libraries under Windows][1], but [supports static link library][2].
To generate a DLL, this can be workaround, refer to Golang [issuse#11058][1]:
- First I had to install a GCC development environment under Windows, and I used [msys2][3].
- Need to configure a quick source, I use [China University of Science and Technology source][4].
- Installing the GCC toolchain:
pacman -S mingw-w64-x86_64-toolchain
(note there is a pit, Msys64 root directory has two mingw64.*
files will cause the Pacman installation to fail, I was violently renamed the conflicting files.) )
- To compile a static link library:
go build -buildmode=c-archive -o libfoo.a foo.go
- Prepare to export the symbol definition file, Sum is the name of the function that needs to be exported:
$ cat foo.defEXPORTS Sum
- Use GCC to turn a static link library into a dynamic link library:
gcc -m64 -shared -o foo.dll foo.def libfoo.a -Wl,--allow-multiple-definition -static -lstdc++ -lwinmm -lntdll -lWs2_32
[1] https://github.com/golang/go/issues/11058
[2] https://github.com/golang/go/issues/13494
[3] http://msys2.github.io/
[4] Https://lug.ustc.edu.cn/wiki/mirrors/help/msys2
Go from 1.5 onwards support compiled C-Call dynamic link library
Shared Librariesgo 1.5 can produce Go GKFX libraries that can is consumed by Go programs. Build the standard library as shared libraries:$ goInstall-buildmode=SharedStdBuild A"Hello, World" program, links against theshared libraries:$ go build-linkshared hello.go$ ls-l hello-rwxr-xr-x 1 ADG ADG 13926 may
02:
HelloGo 1.5 can also build Go programs as C archive files (for Static Lin King) or shared libraries (fordynamic linking), that can is consumed by C programs.
For more detailed usage, refer to
Golang.org/s/execmodes
Try buildmode=c-archive compile to. A. h package for C language use: But not supported x86 only support AMD64
$ go install-buildmode=shared STD
-buildmode=shared not supported on windows/386
Windows not supported ...
https://gocn.io/question/205