c语言学习笔记十四
多目標(biāo)文件的鏈接
示例代碼如下:
test18.c
char stack[512];
int top=-1;
void push(char c){
stack[++top]=c;
}
char pop(){
return stack[top--];
}
int is_empty(void){
return top==-1;
}
?
test19.c
#include <stdio.h>
int a,b=1;
int main(void){
?push('a');
push('b');
push('c');
? ? ? ?while(!is_empty()){
putchar(pop());
}
putchar("\n");
return 0;
}
一起編譯: gcc test19.c test18.c -o test19
分開(kāi)編譯: gcc -c test19.c
? ? ? ? ? ?gcc -c test18.c
? ? ? ? ? ?gcc test19.o test18.o -o test19
運(yùn)行程序:./test19
查看符號(hào)表:readlf -a test19
以下是合并后的符號(hào)表信息?
? [13] .text ? ? ? PROGBITS ? 08048330 000330 00021c 00 ?AX ?0 ? 0 16
? ? ? 52: 08048481 ? ?19 FUNC ? ?GLOBAL DEFAULT ? 13 is_empty
? ? ? 54: 08048466 ? ?27 FUNC ? ?GLOBAL DEFAULT ? 13 pop
? ? ? 68: 080483e4 ? ?88 FUNC ? ?GLOBAL DEFAULT ? 13 main
? ? ? 69: 0804843c ? ?42 FUNC ? ?GLOBAL DEFAULT ? 13 push
? [24] .data ? ? ? PROGBITS ? 0804a00c 00100c 000010 00 ?WA ?0 ? 0 ?4
? ? ? 49: 0804a014 ? ? 4 OBJECT ?GLOBAL DEFAULT ? 24 b
? ? ? 59: 0804a018 ? ? 4 OBJECT ?GLOBAL DEFAULT ? 24 top
? [25] .bss ? ? ? ?NOBITS ? ? 0804a020 00101c 000220 00 ?WA ?0 ? 0 32
? ? ? 66: 0804a028 ? ? 4 OBJECT ?GLOBAL DEFAULT ? 25 a
? ? ? 71: 0804a040 ? 512 OBJECT ?GLOBAL DEFAULT ? 25 stack
查看默認(rèn)鏈接角本:ld --verbose
?
?示例代碼如下:
? ?ENTRY(_start) //整個(gè)程序的入口點(diǎn),可以修改為其他函數(shù)
? ?PROVIDE (__executable_start = SEGMENT_START("text-segment",?
0x08048000)); . = SEGMENT_START("text-segment", 0x08048000) +?
SIZEOF_HEADERS; ? //text Segment的起始地址, 每個(gè)段的格式,段名:{組成}
定義和聲名
?示例代碼:
test19.c
#include <stdio.h>
int a,b=1;
int main(void){
?push('a');
push('b');
push('c');
? ? ? ?while(!is_empty()){
putchar(pop());
}
putchar("\n");
return 0;
}
以上函數(shù)用以下命令編譯會(huì)出問(wèn)題
? gcc -c test19.c -wall
? 錯(cuò)誤信息: ?
?test19.c: 在函數(shù)‘main’中:
?test19.c:4: 警告: 隱式聲明函數(shù)‘push’
?test19.c:8: 警告: 隱式聲明函數(shù)‘is_empty’
?test19.c:9: 警告: 隱式聲明函數(shù)‘pop’
?
??
?函數(shù)聲明沒(méi)有明確指出參數(shù)類型和個(gè)數(shù),不算函數(shù)原型,所以編譯器只能用作隱式聲
明
正確方法:
?#include <stdio.h>
/*聲明用到的幾個(gè)函數(shù)的原型?
?*Extern 關(guān)鍵字表示這個(gè)標(biāo)識(shí)符具有External Linkage.
?* 外部鏈接:external linkage 多個(gè)文件中聲明多次也都代表同一標(biāo)識(shí)符
?* static 作函數(shù)聲明,
?* 內(nèi)部鏈接:internal linkage (某個(gè)文件)中聲明多次也都代表同一標(biāo)識(shí)符
?*/
extern void push(char);
extern char pop(void);
extern int is_empty(void);
/*
?*如果相直接訪問(wèn)test18.c上的top屬性
?*有點(diǎn)像java中的子類的味道extends?
?*它的存儲(chǔ)空間是在test18.c中分配
?* extern在作函數(shù)聲明時(shí)可寫不可寫都行
?* 但在變量聲明時(shí)必需要寫
?*/
extern int top;?
int a,b=1;
int main(void){
?push('a');
push('b');
push('c');
? ? ? ?while(!is_empty()){
putchar(pop());
}
putchar('\n');
return 0;
}
示例代碼:
test18.c
char stack[512];
/*?
?*如果不想讓外界訪問(wèn)可以用static來(lái)修飾
?*有點(diǎn)像java 中的private修飾符
?*/
int top=-1;
void push(char c){
stack[++top]=c;
}
char pop(){
return stack[top--];
}
int is_empty(void){
return top==-1;
}
頭文件
?為了避免重復(fù)的代碼(即在test.19中用作函數(shù)聲明的代碼)
? 可以將其抽象到一個(gè)頭文件中(有點(diǎn)java的味道)
?
示例代碼如下:
test18.h頭文件
/*
?* 此處定義的STACK_H這個(gè)宏是為了防止頭文件被得復(fù)包含
?*/
#ifndef STACK_H
#define STACK_H
extern void push(char);
extern char pop(void);
extern int is_empty(void);
#endihile(!iq
test19.c文件修改為:
#include <stdio.h>
/*
?* <頭文件> 直接從庫(kù)文件中查找
?* "頭文件" 先從當(dāng)前目錄查找,然后到庫(kù)目錄中查詢
?* 這跟c++中有點(diǎn)類似
?* 如果頭文件放在子錄中s
?* 可以用如下格式: #include "子目錄名/test18.h"
?*/
#include "test18.h"
?
int a,b=1;
int main(void){
?push('a');
push('b');
push('c');
? ? ? ?while(!is_empty()){
putchar(pop());
}
putchar('\n');
return 0;
}
注:如果頭文件放到下一子目錄中,需要用-I 參數(shù)指定目錄名
? ? ?命令格如下
? ? ?gcc -c test19.c -I ?子目錄名
? ??
轉(zhuǎn)載于:https://www.cnblogs.com/retacn-yue/archive/2012/09/20/2761233.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
- 上一篇: 2012开博
- 下一篇: JME3中级手册一API特征映射1