静态链接库

来源:互联网 发布:张龙java视频 编辑:程序博客网 时间:2024/05/16 03:14

              静态链接库:编译时就完成链接过程,文件名扩展名为.a

[xxx@localhost staticLibrary]$ lsmain.c  test.c  test.h[mapan@localhost staticLibrary]$ lsmain.c  test.c  test.h[xxx@localhost staticLibrary]$ cat main.c #include<stdio.h>#include"test.h"int main(){   int num=test(2,3);   printf("num=%d\n",num);   return 0;}[xxx@localhost staticLibrary]$ cat test.c#include"test.h"int test(int a,int b){    return a*b;}[xxx@localhost staticLibrary]$ cat test.hint test(int a,int b);[xxx@localhost staticLibrary]$ 

再看:

[xxx@localhost staticLibrary]$ gcc -c test.c[xxx@localhost staticLibrary]$ lsmain.c  test.c  test.h  test.o[xxx@localhost staticLibrary]$ ar cr libtest.a test.o[xxx@localhost staticLibrary]$ lslibtest.a  main.c  test.c  test.h  test.o[xxx@localhost staticLibrary]$ 

调用静态库:

[xxx@localhost staticLibrary]$ gcc main.c  -L. -ltest[xxx@localhost staticLibrary]$ lsa.out  libtest.a  main.c  test.c  test.h  test.o[xxx@localhost staticLibrary]$ ./a.out num=6[xxx@localhost staticLibrary]$ 

查看库信息:

[xxx@localhost staticLibrary]$ ar tv libtest.a rw-rw-r-- 511/511   1240 Jul 26 19:52 2017 test.o[xxx@localhost staticLibrary]$ 

一个静态链接库可以由多个.o文件组成。





原创粉丝点击