go生成so给C调用
lib.go
package main import "C" func main() {} //export Hello func Hello() string { return "Hello" } //export Test func Test() { println("export Test") }
main.c
#include <stdio.h> #include "libgo4c.h" void main() { GoString str; str = Hello(); Test(); printf("%d\n",str.n); }
build.sh
go mod init go4c
go build -buildmode=c-shared -o libgo4c.so lib.go
go build -buildmode=c-archive -o libgo4c.a lib.go
gcc main.c -o test -I./ -L./ -lgo4c export LD_LIBRARY_PATH=`pwd` ./test