chinese直男口爆体育生外卖, 99久久er热在这里只有精品99, 又色又爽又黄18禁美女裸身无遮挡, gogogo高清免费观看日本电视,私密按摩师高清版在线,人妻视频毛茸茸,91论坛 兴趣闲谈,欧美 亚洲 精品 8区,国产精品久久久久精品免费

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

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

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

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

冬至子 ? 來源:計(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è)很小的案例來說明。

首先準(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 編譯的過程中,先編譯得到.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

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ù)的編譯過程比較簡(jiǎn)單,動(dòng)態(tài)庫(kù)相對(duì)復(fù)雜。

審核編輯:劉清

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

    關(guān)注

    180

    文章

    7632

    瀏覽量

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

    關(guān)注

    0

    文章

    78

    瀏覽量

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

    關(guān)注

    0

    文章

    5

    瀏覽量

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

掃碼添加小助手

加入工程師交流群

    評(píng)論

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

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

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

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

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

    【OK210試用體驗(yàn)】構(gòu)建標(biāo)準(zhǔn)c庫(kù)newlib

    構(gòu)建標(biāo)準(zhǔn)c庫(kù)newlib象棋小子 1048272975C語(yǔ)言開發(fā)項(xiàng)目往往需要標(biāo)準(zhǔn)c
    發(fā)表于 11-01 01:39

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

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

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

    C語(yǔ)言和匯編語(yǔ)言有哪些差異?如何利用C語(yǔ)言去編寫單片機(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ā)布程序無需提供靜態(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ǔ)言編寫,c語(yǔ)言的100個(gè)經(jīng)典程序,單片機(jī)的應(yīng)用,開發(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ǔ)言和匯編愛好者學(xué)習(xí)。
    發(fā)表于 12-31 10:16 ?0次下載

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

     、學(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)鍵字說明

    試圖構(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ù)的生成以及使用(樹莓派)

    靜態(tài)庫(kù)優(yōu)點(diǎn): 運(yùn)行快,發(fā)布程序無需提供靜態(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>的生成以及使用(樹莓派)

    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次閱讀