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