1.Go代碼
package main
import "C"
//export SayHello
func SayHello() {
println("Hello from Go!")
}
func main() {}
- 使用
//export
導(dǎo)出函數(shù)旺拉。
2. 編譯成動(dòng)態(tài)庫
2.1 windows
go build -buildmode=c-shared -o mylib.dll
2.2linux
go build -buildmode=c-shared -o mylib.so
- 此時(shí)會(huì)生成對(duì)應(yīng)
動(dòng)態(tài)文件
和.h
文件
3. 編寫c語言測(cè)試文件
#include <stdio.h>
#include "mylib.h"
int main() {
SayHello();
return 0;
}
- 執(zhí)行
gcc test.c mylib.dll -o test.exe
- 運(yùn)行
test.exe