go 工具鏈目前[不支持編譯 windows 下的動態鏈接庫][1],不過[支持靜態鏈接庫][2]


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM