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,感謝博主分享但是對於新手來說,對項目配置可能存在一定困難。下面做了如下參考,僅供新手,勿吐槽!!!![螢幕快照 2017-09-15 11.51.37.png](https://static.studygolang.com/170915/570b45060b82862dfa053938d6e42543.png)項目結構配置![螢幕快照 2017-09-15 11.52.41.png](https://static.studygolang.com/170915/4f85e598d785eb589bd28af515679fc4.png)Project GOPATH配置![螢幕快照 2017-09-15 11.53.03.png](https://static.studygolang.com/170915/3e122db2f13b7b1576fa0f483908ce74.png)Run/Debug Configurations配置2181 次點擊  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.