在用到linux編程的時(shí)候,Makefile可以很好的幫助管理C語言工程,如何構(gòu)建一個(gè)靜態(tài)庫(kù),用一個(gè)很小的案例來說明。
首先準(zhǔn)備需要的文件,以及文件中的內(nèi)容,如下所示
$ cat test1.c
#include
int main()
{
printf("hello world\\n");
return 0;
}
這個(gè).c文件非常簡(jiǎn)單,就是輸出一個(gè)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

現(xiàn)在換種方式實(shí)現(xiàn),采用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文件,同樣可以實(shí)現(xiàn)這個(gè)效果
:~/Documents/clan/test1$ tree
.
├── Makefile
└── test1.c
0 directories, 2 files
:/Documents/clan/test1$ make/Documents/clan/test1$ tree
gcc -c -o test1.o test1.c
gcc -o test1 test1.o
:
.
├── Makefile
├── test1
├── test1.c
└── test1.o
0 directories, 4 files
:~/Documents/clan/test1$ ./test1
hello world

現(xiàn)在將產(chǎn)物刪掉,方便后面的實(shí)驗(yàn)
: ~/Documents/clan/test1$ make clean
rm -f test1 test1.o
:~ /Documents/clan/test1$ tree
.
├── Makefile
└── test1.c
0 directories, 2 files

現(xiàn)在回到gcc 編譯的過程中,先編譯得到.o文件,然后編譯得到靜態(tài)庫(kù)文件,最后通過編譯庫(kù)文件,同樣可以生成可執(zhí)行文件
: ~/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

刪除上述生成的文件
: ~/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

重新編譯Makefile文件,然后執(zhí)行,同樣可以實(shí)現(xiàn),這就是靜態(tài)庫(kù)的實(shí)現(xiàn)方式
: ~/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
上述方式,實(shí)現(xiàn)了一個(gè)非常簡(jiǎn)單的案例,這是在同一目錄層級(jí)下實(shí)現(xiàn)的,如果涉及到多個(gè)目錄層級(jí)呢?
├── 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命令編譯

然后修改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

編譯所有文件,運(yùn)行可執(zhí)行文件,即可輸出結(jié)果
:~/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

要生成Makefile的靜態(tài)庫(kù),只需要在這個(gè)基礎(chǔ)上進(jìn)行修改即可
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

這是一個(gè)非常簡(jiǎn)單的模板案例,靜態(tài)庫(kù)的編譯過程比較簡(jiǎn)單,動(dòng)態(tài)庫(kù)相對(duì)復(fù)雜。
審核編輯:劉清
-
C語言
+關(guān)注
關(guān)注
183文章
7642瀏覽量
144696 -
gcc編譯器
+關(guān)注
關(guān)注
0文章
78瀏覽量
3922 -
Linux編程
+關(guān)注
關(guān)注
0文章
5瀏覽量
742
發(fā)布評(píng)論請(qǐng)先 登錄
C語言標(biāo)準(zhǔn)庫(kù)的基本使用
【OK210試用體驗(yàn)】構(gòu)建標(biāo)準(zhǔn)c庫(kù)newlib
靜態(tài)庫(kù)的優(yōu)點(diǎn)及其靜態(tài)庫(kù)的使用解析
如何利用C語言去調(diào)用rust靜態(tài)庫(kù)呢
100個(gè)經(jīng)典C語言程序
數(shù)碼管(靜態(tài)顯示)【C語言版】
數(shù)碼管(靜態(tài)顯示)【C語言+匯編版】
學(xué)習(xí)C語言的目標(biāo)和方法有哪些及C語言的關(guān)鍵字說明
試圖構(gòu)建一個(gè)便于適配不同平臺(tái)mcu的通用庫(kù)
C語言宏定義與預(yù)處理、函數(shù)和函數(shù)庫(kù)
靜態(tài)庫(kù)和動(dòng)態(tài)庫(kù)的生成以及使用(樹莓派)

如何利用C語言構(gòu)建一個(gè)靜態(tài)庫(kù)呢
評(píng)論