目录格式
clib
-- one.c
-- one.h
main.go
//main.go和clib是同一级,one.c.和one.h是同一级
one.h
int test(int a,int b);
one.c
#include <stdio.h>
int test(int a,int b){
int c=a+b;
printf("c=%d\n",c);
return c;
}
main.go
package main
/*
#cgo LDFLAGS: -Lclib ./clib/one.o
#include "./clib/one.h"
*/
import "C"
import "fmt"
func main() {
var result = C.test(1, 2)
fmt.Println("result=", result)
}
在main.go的目录下运行
1.gcc -c ./clib/one.c -o one.o
2.go build