go 工具链目前[不支持编译 windows 下的动态链接库][1],不过[支持静态链接库][2]

findumars · · 2650 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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

 

    • 准备导出符号定义文件, Sum 就是需要导出的函数名:
      $ cat foo.def
      EXPORTS
          Sum

 

    • 用 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


有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2650 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传