.a與.so區別
阿新 • • 發佈:2019-02-16
簡潔概括.o.a .so檔案的區別
.o: 目標檔案
.a:靜態庫檔案,又稱目標檔案的集合
.so:動態庫檔案
生成方法:
以1.c2.c 3.c為例
.o檔案
gcc-c 1.c -o 1.o(或gcc–c 1.c)
gcc-c 2.c -o 2.o(或gcc–c 2.c)
gcc-c 3.c -o 3.o(或gcc–c 3.c)
.a檔案
ar rcs mylib.a 1.o 2.o 3.o
.so檔案
gcc1.c 2.c 3.c -fPIC -shared -o libmytest.so
呼叫方法:
1.c
#include“my.h”
voidprintCat()
{
printf(“MiaoMiao ~ ~ ~ ~\n”);
}
my.h
#include<stdio.h>
voidprintCat(); //1.c
voidprintDog(); //2.c
voidprintPig(); //3.c
=====================================
test.c
#include“my.h”
int main()
{
printCat();
return0;
}
.a檔案的呼叫:gcc
test.c -o test mylib.a
.so檔案的呼叫:gcc test.c -o test -L. -lmytest
注:若想將.so檔案作為共享庫,需要將.so檔案放到特定的目錄下面/usr/local/lib,但即使這樣編譯還是會報錯,需要使用ldconfig命令來更新/ect/ld.so.cache才會生效