c語言學習筆記十五

2021-09-01 10:54:10 字數 1630 閱讀 4766

靜態庫

在一定場景下將一些**編譯成乙個庫檔案,以供其他程式使用

示例**如下:

20-23檔案放在子目錄stack中

test20.c檔案:

char stack[512];

int top=-1;

test21.c檔案:

extern char stack[512];

extern int top;

void push(char c)

test22.c檔案:

extern char stack[512];

extern int top;

char pop(void)

test23.c檔案:

extern char stack[512];

extern int top;

int is_empty(void)

test18.h標頭檔案

#ifndef stack_h

#define stack_h

extern void push(char);

extern char pop(void);

extern int is_empty(void);

#endif

test24.c檔案

#include

#include "test18.h"

int main(void)

1 將test20,test21,test22,test23編譯成目標檔案:

gcc -c stack/test20.c stack/test21.c stack/test22.c stack/test23.c

2 然後打包成乙個靜態庫:

//ar 打包

//引數r 表示將後面檔案新增到檔案包中

//引數s 表示生成靜態庫

ar rs libstack.a test20.o test21.o test22.o test23.o

/** -l. 在當前目錄查詢庫檔案

* -lstack 鏈結libstack庫

* -istack 標頭檔案所在目錄

*/gcc test24.c -l. -lstack -istack -o test24

也可以將目標檔案一起編譯:

gcc test24.c

檢視編譯器缺省會test20.o test21.o test22.o test23.o -istack -0 test24

查詢的目錄:

gcc -print-search-dirs

共享庫只在執行時作動態鏈結;

鏈結靜態庫時,鏈結器會把靜態庫的目標檔案取出來和可執行檔案鏈結在一起

可以使用反江編檢視:objdump -d test24

示例**如下:

080483b4 :

80483b4:

55 push %ebp

80483b5:

89 e5

mov %esp,%ebp

080483d0 :

80483d0:

55 push %ebp

80483d1:

89 e5

mov %esp,%ebp

80483d3:

83 ec 04

sub $0x4,%esp

c語言學習筆記十五

靜態庫 在一定場景下將一些 編譯成乙個庫檔案,以供其他程式使用 示例 如下 20 23檔案放在子目錄stack中 test20.c檔案 char stack 512 int top 1 test21.c檔案 extern char stack 512 extern int top void push...

C語言學習 十五 共用體

共用體是一種特殊的資料型別,允許相同的記憶體位置儲存不同的資料型別,可以定義乙個帶有多成員的共用體,但是任何時候只能有乙個成員帶有值,共用體提供了一種使用相同的記憶體位置的有效方式.共用體的作用主要是節省記憶體.為了定義共用體,必須使用union語句,方式與定義結構類似,union語句定義了乙個新的...

C語言學習筆記

include include void swap int p1,int p2 void swapa int arr,int n void printfa int arr,int n int main int argc,char argv swap i,j printfa array,6 swapa...