Golang調用動態庫so
來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。測試動態庫test_so.h```int test_so_func(int a,int b);```test_so.c```#include "test_so.h"int test_so_func(int a,int b){ return a*b;}```產生so```gcc -shared ./test_so.c -o test_so.so```複製so檔案到golang項目目錄 golang項目目錄,建立load_so.h```int do_test_so_func(int a,int b);```load_so.c```#include "load_so.h"#include <dlfcn.h>int do_test_so_func(int a,int b){ void* handle; typedef int (*FPTR)(int,int); handle = dlopen("./test_so.so", 1); FPTR fptr = (FPTR)dlsym(handle, "test_so_func"); int result = (*fptr)(a,b); return result;} ```test.go```package main/*#include "load_so.h"#cgo LDFLAGS: -ldl*/import "C"import "fmt"func main() { fmt.Println("20*30=", C.do_test_so_func(20, 30)) fmt.Println("hello world")}```編譯運行即可。####以上主要參考https://studygolang.com/articles/67,感謝博主分享但是對於新手來說,對項目配置可能存在一定困難。下面做了如下參考,僅供新手,勿吐槽!!!項目結構配置Project GOPATH配置Run/Debug Configurations配置2181 次點擊