root@ubuntu:/home/cpptest/gotest# gcc -fPIC -shared -o libhi.so hi.c -lm
root@ubuntu:/home/cpptest/gotest# go build -ldflags="-r ./" testc.go
root@ubuntu:/home/cpptest/gotest# ./testc
hello world C!
Hello c, welcome to go!
20*30= 600
Ci(0.50)= -0.1777841
Si(1.00)= 0.9460831
Ei(1.00)= -0.2193839
a= [2 -1 0 0 1 1 1 0 0]
det(a)=-1.000000
a^-1= [0 0 1 -1 0 2 1 1 -2]
aa^-1= [1 0 0 0 1 0 0 0 1]
det(a^-1)=-1.000000
c= [32 32 43 40 40 66 100 103 150]
c1= [32 32 43 40 40 66 100 103 150]
package main
/*
#cgo CFLAGS: -I.
#cgo LDFLAGS: -L. -lhi -ldl
#include "hi.h"
*/
import "C" // 注意这个地方与上面注释的地方不能有空行,并且不能使用括号如import ("C" "fmt")
import "fmt"
func main(){
C.hi()
fmt.Println("Hello c, welcome to go!")
fmt.Println("20*30=", C.test_so_func(20, 30))
//fmt.Println("20*30=", C.do_test_so_func(20, 30))
{
//_lcoss@8计算余弦积分
var x float64=0.5
y:=C.lcoss(C.double(x))
fmt.Printf("Ci(%4.2f)=%12.7f\n", x,float64(y))
}
{
//_lsinn@8计算大写的正弦积分Si(x)=∫[0->x]sint/tdt=si(x)+pi/2,Si(1.00)
var x float64=1.0
y:=C.lsinn(C.double(x))
fmt.Printf("Si(%4.2f)=%12.7f\n", x,float64(y))
}
{
//_lexpp@8计算指数积分
var x float64=1.0
y:=C.lexpp(C.double(x))
fmt.Printf("Ei(%4.2f)=%12.7f\n", x,float64(y))
}
{
var a []C.double=[]C.double{
2,-1,0,
0,1,1,
1,0,0}
var a1 []C.double=[]C.double{
2,-1,0,
0,1,1,
1,0,0}
var a2 []C.double=[]C.double{
2,-1,0,
0,1,1,
1,0,0}
fmt.Println("a=",a)
// 求行列式值的全选主元高斯消去法
det := C.bsdet(&a1[0],3)
fmt.Printf("det(a)=%f\n",float64(det))
// 实矩阵求逆的全选主元高斯约当法(Gauss-Jordan)
i:=C.brinv(&a2[0],3)
if(i!=0){
fmt.Println("a^-1=",a2)
}
var c []C.double=[]C.double{0,0,0,0,0,0,0,0,0}
C.brmul(&a[0],&a2[0],3,3,3,&c[0])
fmt.Println("aa^-1=",c)
det = C.bsdet(&a2[0],3)
fmt.Printf("det(a^-1)=%f\n",float64(det))
}
{
var a []C.double=[]C.double{
2,3,1,
4,2,6,
7,8,9}
var b []C.double=[]C.double{
5,3,6,
7,8,9,
1,2,4}
var c []C.double=[]C.double{0,0,0,0,0,0,0,0,0}
C.brmul(&a[0],&b[0],3,3,3,&c[0])
fmt.Println("c=",c)
c1:=c[:]
fmt.Println("c1=",c1)
}
}
root@ubuntu:/home/cpptest/gotest# gcc -shared ./test_so.c -o test_so.so
root@ubuntu:/home/cpptest/gotest# gcc -fPIC -shared -o libhi.so hi.c
root@ubuntu:/home/cpptest/gotest# go build -ldflags="-r ./" testc.go
root@ubuntu:/home/cpptest/gotest# ./testc
root@ubuntu:/home/cpptest/gotest# go build -ldflags="-r ./" testc.go
root@ubuntu:/home/cpptest/gotest# ./testc
hello world C!
Hello c, welcome to go!
20*30= 600
20*30= 600
c代码hi.h:
void hi();
int test_so_func(int a,int b);
int do_test_so_func(int a,int b);
c代码hi.c:
#include <stdio.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;
}
void hi(){
printf("hello world C!\n");
}
int test_so_func(int a,int b)
{
return a*b;
}
go代码testc.go:
package main
/*
#cgo CFLAGS: -I.
#cgo LDFLAGS: -L. -lhi -ldl
#include "hi.h"
*/
import "C" // 注意这个地方与上面注释的地方不能有空行,并且不能使用括号如import ("C" "fmt")
import "fmt"
func main(){
C.hi()
fmt.Println("Hello c, welcome to go!")
fmt.Println("20*30=", C.test_so_func(20, 30))
fmt.Println("20*30=", C.do_test_so_func(20, 30))
}
原文:https://www.cnblogs.com/Ivanhan2019/p/12758554.html