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

 

[xxx@localhost staticLibrary]$ ls
main.c  test.c  test.h
[mapan@localhost staticLibrary]$ ls
main.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.h
int test(int a,int b);

[xxx@localhost staticLibrary]$ 

 

再看:

 

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

 

调用静态库:

 

[xxx@localhost staticLibrary]$ gcc main.c  -L. -ltest
[xxx@localhost staticLibrary]$ ls
a.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文件组成。