这篇文章主要记录了如何在mac系统上创建一个.so库文件,然后调用so中的函数测试之。
环境:mac系统
编译器:gcc
编译方式:命令行
第一步:创建一个文件:【addso.c】,内容如下:
#include<stdio.h>
int add(int a, int b)
{
printf("hello, add(a + b)");
return a + b;
}
第二步:编译该文件,产生一个【.so】库文件,命令如下:
gcc -fPIC -shared addso.c -o libaddso.so
ok,到现在为止,so库生成出来了,下面我们编写一个脚本调用它。
第三步:创建一个文件【addso_main.c】,内容如下:
#include<stdio.h>
int add(int, int);//必须声明有这样一个函数,否则肯定报错。
int main(void)
{
add(2,3);
return 0;
}
第四步:编译该文件(编译时,肯定需要指定我们需要链接的库文件)
gcc -L. -laddso addso_main.c -o add
第五步:运行我们生成的add文件。
➜ mac_so_test ./add
hello, add(a + b)