Linux建立静态链接库的步骤

时间:2022-07-22 06:57:24 来源:网络 发布:手游网 浏览:164次

通常我们所说的静态链接库就是.lib文件,文件一般比较大,能够将函数和数据编译到一个.lib文件中,那么Linux系统要如何建立静态链接库呢?下面小编就给大介绍下Linux建立静态链接库的方法。

主要有两步:

1.编译源码

2.制作库

=====================================

以两个简单的源码为例,示范静态库制作的具体流程

1.编译源码

hello_first.c

void hello_first(void)

{

printf(“hello first”);

}

hello_second.c

void hello_second(void)

{

printf(“hello second”);

}

编译:

gcc -c hello_first.c -o hello_first.o

gcc -c hello_second.c -o hello_second.o

2.制作静态库

ar -r libhello.a hello_first.o hello_second.o

具体调用库实例

hello_main.c

#include 《stdio.h》

void hello_first(void);

void hello_second(void);

int main()

{

hello_first();

hello_second();

return 0;

}

编译:

第一种方法:

gcc hello_main.c libhello.a -o hello_main

第二中方法:

拷贝libhello.a到/lib目录下

gcc hello_main.c -lhello -o hello_main2

  运行截图:

Linux建立静态链接库的步骤

OK!静态库调用成功!!

思考:

静态库作用是什么?为甚么要发明它?

1.静态库主要是用于方便程序员编程,将要使用的函数封装成库,只要告诉他们函数接口就可以了,这样程序员只要知道怎么调用就行了。而厂家可以保持接口不变,而对代码进行修改维护。

2.另外有可能是程序员自己将自己经常用到的函数封装起来,不用重复编写,只需调用前声明下就可以了。

上面就是Linux建立静态链接库的相关介绍了,在制作静态链接库的时候需要先编译源码,然后再制作静态链接库,制作好后需运行监测。

评论
评论
发 布