首页 > 技术知识 > 正文

简洁概括.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

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才会生效

猜你喜欢