一区二区三区三上|欧美在线视频五区|国产午夜无码在线观看视频|亚洲国产裸体网站|无码成年人影视|亚洲AV亚洲AV|成人开心激情五月|欧美性爱内射视频|超碰人人干人人上|一区二区无码三区亚洲人区久久精品

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫(xiě)文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

如何利用C語(yǔ)言構(gòu)建一個(gè)靜態(tài)庫(kù)呢

冬至子 ? 來(lái)源:計(jì)算機(jī)科學(xué)實(shí)驗(yàn)室 ? 作者:好壞生長(zhǎng) ? 2023-01-18 11:20 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

在用到linux編程的時(shí)候,Makefile可以很好的幫助管理C語(yǔ)言工程,如何構(gòu)建一個(gè)靜態(tài)庫(kù),用一個(gè)很小的案例來(lái)說(shuō)明。

首先準(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

2.jpg

現(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
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

現(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

2.jpg

現(xiàn)在回到gcc 編譯的過(guò)程中,先編譯得到.o文件,然后編譯得到靜態(tài)庫(kù)文件,最后通過(guò)編譯庫(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

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文件,然后執(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命令編譯

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

編譯所有文件,運(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

2.jpg

要生成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

2.jpg

這是一個(gè)非常簡(jiǎn)單的模板案例,靜態(tài)庫(kù)的編譯過(guò)程比較簡(jiǎn)單,動(dòng)態(tài)庫(kù)相對(duì)復(fù)雜。

審核編輯:劉清

聲明:本文內(nèi)容及配圖由入駐作者撰寫(xiě)或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • C語(yǔ)言
    +關(guān)注

    關(guān)注

    180

    文章

    7632

    瀏覽量

    141759
  • gcc編譯器
    +關(guān)注

    關(guān)注

    0

    文章

    78

    瀏覽量

    3747
  • Linux編程
    +關(guān)注

    關(guān)注

    0

    文章

    5

    瀏覽量

    696
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評(píng)論

    相關(guān)推薦
    熱點(diǎn)推薦

    C語(yǔ)言標(biāo)準(zhǔn)庫(kù)的基本使用

    寫(xiě)出看起來(lái)專業(yè)的C代碼,除了規(guī)范的變量/函數(shù)命名,還需要熟練使用C語(yǔ)言的標(biāo)準(zhǔn)庫(kù)。當(dāng)為了數(shù)組拷貝自己編寫(xiě)
    發(fā)表于 09-14 14:04 ?967次閱讀

    介紹個(gè)C語(yǔ)言編寫(xiě)的硬件外設(shè)訪問(wèn)庫(kù)

    今天要介紹的開(kāi)源軟件叫 c-periphery,個(gè)C 語(yǔ)言編寫(xiě)的硬件外設(shè)訪問(wèn)庫(kù)。
    的頭像 發(fā)表于 10-26 10:36 ?1780次閱讀
    介紹<b class='flag-5'>一</b><b class='flag-5'>個(gè)</b>用<b class='flag-5'>C</b><b class='flag-5'>語(yǔ)言</b>編寫(xiě)的硬件外設(shè)訪問(wèn)<b class='flag-5'>庫(kù)</b>

    自己如何利用C語(yǔ)言封裝個(gè)TRACE函數(shù)?

    自己如何利用C語(yǔ)言封裝個(gè)TRACE函數(shù)?
    發(fā)表于 10-18 09:03

    如何利用C語(yǔ)言去編寫(xiě)單片機(jī)程序

    C語(yǔ)言和匯編語(yǔ)言有哪些差異?如何利用C語(yǔ)言去編寫(xiě)單片機(jī)程序
    發(fā)表于 11-02 09:59

    靜態(tài)庫(kù)的優(yōu)點(diǎn)及其靜態(tài)庫(kù)的使用解析

    、靜態(tài)庫(kù)優(yōu)點(diǎn):運(yùn)行快,發(fā)布程序無(wú)需提供靜態(tài)庫(kù),因?yàn)橐呀?jīng)在app中,移植方便缺點(diǎn):更新慢 繁瑣1、靜態(tài)
    發(fā)表于 02-17 07:45

    如何利用C語(yǔ)言去調(diào)用rust靜態(tài)庫(kù)

    持續(xù)。rust整個(gè)結(jié)構(gòu)還不是特別清晰,特別是庫(kù)和wrapper相關(guān)的同個(gè)C項(xiàng)目,包含多個(gè)rust的靜態(tài)庫(kù),
    發(fā)表于 06-21 10:27

    100個(gè)經(jīng)典C語(yǔ)言程序

    c語(yǔ)言編寫(xiě),c語(yǔ)言的100個(gè)經(jīng)典程序,單片機(jī)的應(yīng)用,開(kāi)發(fā)利用
    發(fā)表于 12-17 11:46 ?11次下載

    數(shù)碼管(靜態(tài)顯示)【C語(yǔ)言版】

    數(shù)碼管(靜態(tài)顯示)【C語(yǔ)言版】數(shù)碼管(靜態(tài)顯示)【C語(yǔ)言版】數(shù)碼管(
    發(fā)表于 12-29 15:27 ?0次下載

    數(shù)碼管(靜態(tài)顯示)【C語(yǔ)言+匯編版】

    數(shù)碼管(靜態(tài)顯示)【C語(yǔ)言+匯編版】,多種集合,符合C語(yǔ)言和匯編愛(ài)好者學(xué)習(xí)。
    發(fā)表于 12-31 10:16 ?0次下載

    學(xué)習(xí)C語(yǔ)言的目標(biāo)和方法有哪些及C語(yǔ)言的關(guān)鍵字說(shuō)明

     、學(xué)習(xí)C語(yǔ)言的目標(biāo)主要是:1. 熟練掌握C語(yǔ)言的關(guān)鍵字,語(yǔ)法規(guī)則,程序控制等;2. 掌握基本的數(shù)據(jù)結(jié)構(gòu),數(shù)組、鏈表、棧和隊(duì)列等;3. 掌
    發(fā)表于 08-02 17:34 ?1次下載
    學(xué)習(xí)<b class='flag-5'>C</b><b class='flag-5'>語(yǔ)言</b>的目標(biāo)和方法有哪些及<b class='flag-5'>C</b><b class='flag-5'>語(yǔ)言</b>的關(guān)鍵字說(shuō)明

    試圖構(gòu)建個(gè)便于適配不同平臺(tái)mcu的通用庫(kù)

    試圖構(gòu)建個(gè)便于適配不同平臺(tái)mcu的通用庫(kù)
    發(fā)表于 11-26 15:21 ?10次下載
    試圖<b class='flag-5'>構(gòu)建</b><b class='flag-5'>一</b><b class='flag-5'>個(gè)</b>便于適配不同平臺(tái)mcu的通用<b class='flag-5'>庫(kù)</b>

    C語(yǔ)言宏定義與預(yù)處理、函數(shù)和函數(shù)庫(kù)

    目錄前言、C語(yǔ)言預(yù)處理二、宏定義三、函數(shù)四、函數(shù)庫(kù)五、自己制作靜態(tài)鏈接庫(kù)(ubuntu 環(huán)境下
    發(fā)表于 12-07 21:06 ?3次下載
    <b class='flag-5'>C</b><b class='flag-5'>語(yǔ)言</b>宏定義與預(yù)處理、函數(shù)和函數(shù)<b class='flag-5'>庫(kù)</b>

    靜態(tài)庫(kù)和動(dòng)態(tài)庫(kù)的生成以及使用(樹(shù)莓派)

    、靜態(tài)庫(kù)優(yōu)點(diǎn): 運(yùn)行快,發(fā)布程序無(wú)需提供靜態(tài)庫(kù),因?yàn)橐呀?jīng)在app中,移植方便缺點(diǎn):更新慢 繁瑣1、
    發(fā)表于 12-22 18:44 ?0次下載
    <b class='flag-5'>靜態(tài)</b><b class='flag-5'>庫(kù)</b>和動(dòng)態(tài)<b class='flag-5'>庫(kù)</b>的生成以及使用(樹(shù)莓派)

    C語(yǔ)言動(dòng)態(tài)庫(kù)靜態(tài)庫(kù)

    C語(yǔ)言動(dòng)態(tài)庫(kù)靜態(tài)庫(kù)
    的頭像 發(fā)表于 02-06 09:45 ?1735次閱讀

    Linux中的靜態(tài)庫(kù)和共享庫(kù)

    庫(kù)個(gè)二進(jìn)制文件,包含的代碼可被程序調(diào)用。例如標(biāo)準(zhǔn)C庫(kù)、數(shù)學(xué)庫(kù)、線程
    的頭像 發(fā)表于 05-10 09:34 ?1332次閱讀