背景
go语言编写代码,底层需要一调用C?C++的so库,避免自己再去造轮子,所以想直接使用golang调用so,参考了其他博客大佬写的,中间出现过很多错误,都记录下来供大家参考
环境准备
因为要将c语言打包,会需要借助gcc的工具,可以直接网上下载安装,还有go工具,也可以在网上直接下载安装
sudo apt-get update
sudo apt install gcc
sudo apt install golang-go
操作步骤
1.创建目录结构如下

2.创建c文件和h文件
创建mylibrary.c
#include <stdio.h>
int addtest(int a,int b) {
int c = a+b;
printf("Hello from C!\n");
printf("a + b =%d \n",c);
return c;
}
创建mylibrary.h
#ifndef MYLIBRARY_H
#define MYLIBRARY_H
int addtest(int a,int b);
#endif
- 编译生成.so
gcc -shared -o ./lib/libmyprint.so include/mylibrary.c
有的文章提到,编译需要加上-fPIC 参数,应该可以不加
4.编写go文件
go设置 需要打开CGO<



被折叠的 条评论
为什么被折叠?



