go創建動態庫
阿新 • • 發佈:2017-12-08
c編譯器 tac min share .so sha gcc 生成 include
- nix
nix創建so比較方便,寫好go代碼之後,直接一條命令搞定。
go build -buildmode=c-shared -o libgobblob.so
命令執行之後,會生成libgobblob.h和 libgobblob.so
- windows
windows需要安裝gcc編譯器,我用的的MinGW包,解壓,把bin目錄加入環境變量。
然後執行命令之後發現會報錯,windows下go不支持生成動態庫。
>>go build -buildmode=c-shared -o libgobblob.dll -buildmode=c-shared not supported on windows/amd64
這一步折騰了好久,最終在stackoverflow找到了解決方法。[[ https://stackoverflow.com/questions/40573401/building-a-dll-with-go-1-7 | building-a-dll-with-go]]
- 編譯靜態庫
go build -buildmode=c-archive -o libgobblob.a
- gobblob.c文件,然後把go代碼中要導出的函數,在gobblob.c中全部調用一遍。
#include <stdio.h> #include "libgobblob.h" // force gcc to link in go runtime (may be a better solution than this) void dummy() { // 所有在go中要導出的代碼都在這裏調用一次,參數隨便寫,只要能編譯ok即可 gobblob_init(NULL,NULL,NULL); gobblob_deinit(NULL); gobblob(NULL,NULL,NULL,NULL,NULL,NULL); } int main() { }
執行如下命令,生成dll
gcc -shared -pthread -o libgobblob.dll gobblob.c libgobblob.a -lWinMM -lntdll -lWS2_32 -Iinclude
go創建動態庫