全球最实用的IT互联网信息网站!

AI人工智能P2P分享&下载搜索网页发布信息网站地图

当前位置:诺佳网 > 电子/半导体 > 模拟技术 >

如何利用C语言构建一个静态库呢

时间:2023-01-18 11:20

人气:

作者:admin

标签: 构建  语言  如何  利用  一个 

导读:现在回到gcc 编译的过程中,先编译得到.o文件,然后编译得到静态库文件,最后通过编译库文件,同样可以生成可执行文件...

在用到linux编程的时候,Makefile可以很好的帮助管理C语言工程,如何构建一个静态库,用一个很小的案例来说明。

首先准备需要的文件,以及文件中的内容,如下所示

$ cat test1.c 
#include 


int main()
{
  printf("hello world\\n");


  return 0;
}

这个.c文件非常简单,就是输出一个hello world。用gcc编译,就能输出。

`

: ~/Documents/clan/test1$ gcc test1.c

:~ /Documents/clan/test1$ tree

.

├── a.out

└── test1.c

0 directories, 3 files

:~/Documents/clan/test1$ ./a.out

hello world

2.jpg

现在换种方式实现,采用Makefile的形式,编辑Makefile的脚本

:~/Documents/clan/test1$ rm a.out 
:~/Documents/clan/test1$ cat Makefile 
test1 : test1.o
  gcc -o test1 test1.o


test1.o : test1.c
  gcc -c -o test1.o test1.c


clean :
  rm -f test1 test1.o

编译Makefile文件,同样可以实现这个效果

:~/Documents/clan/test1$ tree
.
├── Makefile
└── test1.c

0 directories, 2 files
:/Documents/clan/test1$ make
gcc -c -o test1.o test1.c
gcc -o test1 test1.o
:
/Documents/clan/test1$ tree
.
├── Makefile
├── test1
├── test1.c
└── test1.o

0 directories, 4 files
:~/Documents/clan/test1$ ./test1
hello world

2.jpg

现在将产物删掉,方便后面的实验

: ~/Documents/clan/test1$ make clean

rm -f test1 test1.o

:~ /Documents/clan/test1$ tree

.

├── Makefile

└── test1.c

0 directories, 2 files

2.jpg

现在回到gcc 编译的过程中,先编译得到.o文件,然后编译得到静态库文件,最后通过编译库文件,同样可以生成可执行文件

: ~/Documents/clan/test1$ gcc -c -o test1.o test1.c

:~ /Documents/clan/test1$ tree

.

├── Makefile

├── test1.c

└── test1.o

0 directories, 3 files

: ~/Documents/clan/test1$ ar -cr libtest1.a test1.o

:~ /Documents/clan/test1$ tree

.

├── libtest1.a

├── Makefile

├── test1.c

└── test1.o

0 directories, 4 files

: ~/Documents/clan/test1$ gcc libtest1.a

:~ /Documents/clan/test1$ tree

.

├── a.out

├── libtest1.a

├── Makefile

├── test1.c

└── test1.o

0 directories, 5 files

:~/Documents/clan/test1$ ./a.out

hello world

2.jpg

删除上述生成的文件

: ~/Documents/clan/test1$ rm a.out

:~ /Documents/clan/test1$ rm libtest1.a

: ~/Documents/clan/test1$ rm test1.o

:~ /Documents/clan/test1$ tree

.

├── Makefile

└── test1.c

0 directories, 2 files

重新编辑Makefile文件

test1 : libtest1.a

gcc -o test1 libtest1.a

libtest1.a : test1.o

ar -cr libtest1.a test1.o

test1.o : test1.c

gcc -c -o test1.o test1.c

clean :

rm -f test1 test1.o libtest1.a

2.jpg

重新编译Makefile文件,然后执行,同样可以实现,这就是静态库的实现方式

: ~/Documents/clan/test1$ make

gcc -c -o test1.o test1.c

ar -cr libtest1.a test1.o

gcc -o test1 libtest1.a

:~ /Documents/clan/test1$ tree

.

├── libtest1.a

├── Makefile

├── test1

├── test1.c

└── test1.o

0 directories, 5 files

:~/Documents/clan/test1$ ./test1

hello world

上述方式,实现了一个非常简单的案例,这是在同一目录层级下实现的,如果涉及到多个目录层级呢?

├── func

│ ├── func.c

│ └── func.h

├── Makefile

└── test1.c

其中func.c文件的代码如下

#include "func.h"

int add(int a, int b){return a+b;}

func.h文件的代码

int add(int a, int b);

test.c文件的代码

#include

#include "func/func.h"

int main()

{

printf("hello world\\n");

printf("%d\\n",add(10,20));

return 0;

}

用gcc命令编译

2.jpg

然后修改Makefile文件

:~/Documents/clan/test1$ cat Makefile

test1 : test1.o func/func.o

gcc -o test1 test1.o func/func.o

test1.o : test1.c

gcc -c -o test1.o test1.c

func/func.o : func/func.c func/func.h

gcc -c -o func/func.o func/func.c

clean :

rm -f test1 test1.o func/func.o

2.jpg

编译所有文件,运行可执行文件,即可输出结果

:~/Documents/clan/test1$ tree

.

├── func

│ ├── func.c

│ └── func.h

├── Makefile

└── test1.c

1 directory, 4 files

: ~/Documents/clan/test1$ make

gcc -c -o test1.o test1.c

gcc -c -o func/func.o func/func.c

gcc -o test1 test1.o func/func.o

:~ /Documents/clan/test1$ tree

.

├── func

│ ├── func.c

│ ├── func.h

│ └── func.o

├── Makefile

├── test1

├── test1.c

└── test1.o

1 directory, 7 files

:~/Documents/clan/test1$ ./test1

hello world

30

2.jpg

要生成Makefile的静态库,只需要在这个基础上进行修改即可

test1 : libtest1.a func/libfunc.a

gcc -o test1 libtest1.a func/libfunc.a

libtest1.a : test1.o

ar -cr libtest1.a test1.o

func/libfunc.a : func/func.o

ar -cr func/libfunc.a func/func.o

test1.o : test1.c

gcc -c -o test1.o test1.c

func/func.o : func/func.c func/func.h

gcc -c -o func/func.o func/func.c

clean :

rm -f test1 test1.o libtest1.a func/libfunc.a func/func.o

2.jpg

这是一个非常简单的模板案例,静态库的编译过程比较简单,动态库相对复杂。

审核编辑:刘清

温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信