go 工具链目前[不支持编译 windows 下的动态链接库][1],不过[支持静态链接库][2]。 想要产生dll,可以这样 workaround ,参考 golang [issuse#11058][1]:
- 首先得装一个 windows 下的 gcc 开发环境,我用了 [msys2][3] 。
- 需要配置一个快点的源,我用[中国科技大学的源][4]。
- 安装 gcc 工具链:
pacman -S mingw-w64-x86_64-toolchain (注意这里有个坑, msys64 根目录有两个 mingw64.* 文件会导致 pacman 安装失败,我是暴力重命名了冲突的文件。)
- 编译静态链接库:
go build -buildmode=c-archive -o libfoo.a foo.go
- 用 gcc 把静态链接库转成动态链接库:
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从1.5开始就支持编译编译C调用的动态链接库
Shared libraries
Go 1.5 can produce Go shared libraries that can be consumed by Go programs.
Build the standard library as shared libraries:
$ go install -buildmode=shared std
Build a "Hello, world" program that links against the shared libraries:
$ go build -linkshared hello.go
$ ls -l hello
-rwxr-xr-x 1 adg adg 13926 May 26 02:13 hello
Go 1.5 can also build Go programs as C archive files (for static linking) or shared libraries (for dynamic linking) that can be consumed by C programs.
更多详细的用法可以参考 golang.org/s/execmodes
试试看 buildmode=c-archive 编译到 .a .h 包给 c 语言使用。。但是不支持 x86 只支持 amd64
$ go install -buildmode=shared std -buildmode=shared not supported on windows/386
不支持 Windows...
https://gocn.io/question/205
|
请发表评论