so文件可以理解为一个函数机器码文件。这个文件中的机器码不能直接执行。需要依赖另一个有main函数的程序进行调用才能执行。
举个例子
新建头文件test.h
声明say_hello函数
#include <stdio.h>
void say_hello();
新建源文件test.c
实现say_hello函数
#include "test.h"
void say_hello(char *name){
printf("hello %s\n",name);
}
编译test.c生成so文件
gcc test.c -fPIC -shared -o libtest.so
新建源文件main.c调用libtest.so文件中的中的say_hello函数
#include "test.h"//1.引入头文件函数
int main(){
say_hello("guanxianseng");
return 0;
}
编译main.c链接libtets.so文件生成main 可执行文件。
say_hello方法实质上就是libtset.so中的say_hello方法。
gcc main.c -L. -ltest -o main
然后
cp libtest.so /usr/local/lib
再执行ldconfig
最后运行main
成功输出hello world
https://blog.csdn.net/deeplan_1994/article/details/83927832