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
#include <stdlib.h>
int main()
{
int var_runoob = 10;
int *p;//定义指针变量p,变量p编译转化的栈内存地址指向的栈内存中存得是变量var_runoob编译转化的栈内存地址
p = &var_runoob;//不要栈内存的值,直接把var_runoob的栈内存地址给p
printf("变量名编译后,转成的的栈内存地址:var_runoob=> %p,p=> %p", p,&p);
return 0;
}
变量名编译后,转成的的栈内存地址:var_runoob=> 0x7ffd1ed0c16c,p=> 0x7ffd1ed0c160
变量名 变量名编译后栈内存地址 栈内存值
var_runoob 0x7ffd1ed0c16c 10
p 0x7ffd1ed0c160 0x7ffd1ed0c16c