This is a creation in Article, where the information may have evolved or changed.
Test the Dynamic Library
Test_so.h
int test_so_func (int A,int b);
Test_so.c
" test_so.h " int test_so_func (int A,int b) { return A *b;}
Generate so
gcc -shared./test_so.c-o test_so.so
Copy so files to the Golang project directory
Golang project directory, establish
Load_so.h
int do_test_so_func (int A,int b);
Load_so.c
#include"load_so.h"#include<dlfcn.h>intDo_test_so_func (intAintb) { void*handle; typedefint(*FPTR) (int,int); Handle= Dlopen ("./test_so.so",1); Fptr fptr= (fptr) dlsym (handle,"Test_so_func"); intresult = (*fptr) (A, B); returnresult;}
Test.go
Package Main /* #include "load_so.h" #cgo ldflags:-ldl */ "C""fmt"func Main () { Fmt. Println ("20*30=", C.do_test_so_func ())}
Compile and run.
Note: A great God in the Go call C, fell into a hole, address http://www.newjueqi.com/?p=106, the conclusion is
1./* */Comment The code next line must be import "C", middle cannot have blank line
2.import "C" must be a separate line and cannot be imported with other libraries