轉(zhuǎn)載: |
#include文件的一個(gè)不利之處在于一個(gè)頭文件可能會(huì)被多次包含,為了說(shuō)明這種錯(cuò)誤,考慮下面的代碼:
#include "x.h"
#include "x.h"
顯然,這里文件x.h被包含了兩次,沒(méi)有人會(huì)故意編寫(xiě)這樣的代碼。但是下面的代碼:
#include "a.h"
#include "b.h"
看上去沒(méi)什么問(wèn)題。如果a.h和b.h都包含了一個(gè)頭文件x.h。那么x.h在此也同樣被包含了兩次,只不過(guò)它的形式不是那么明顯而已。
多重包含在絕大多數(shù)情況下出現(xiàn)在大型程序中,它往往需要使用很多頭文件,因此要發(fā)現(xiàn)重復(fù)包含并不容易。要解決這個(gè)問(wèn)題,我們可以使用條件編譯。如果所有的頭文件都像下面這樣編寫(xiě):
#ifndef _HEADERNAME_H
#define _HEADERNAME_H
...
#endif
那么多重包含的危險(xiǎn)就被消除了。當(dāng)頭文件第一次被包含時(shí),它被正常處理,符號(hào)_HEADERNAME_H被定義為1。如果頭文件被再次包含,通過(guò)條件編譯,它的內(nèi)容被忽略。符號(hào)_HEADERNAME_H按照被包含頭文件的文件名進(jìn)行取名,以避免由于其他頭文件使用相同的符號(hào)而引起的沖突。
但是,你必須記住預(yù)處理器仍將整個(gè)頭文件讀入,即使這個(gè)頭文件所有內(nèi)容將被忽略。由于這種處理將托慢編譯速度,所以如果可能,應(yīng)該避免出現(xiàn)多重包含。
下面幾個(gè)實(shí)例:
test-1.0使用#ifndef只是防止了頭文件被重復(fù)包含(其實(shí)本例中只有一個(gè)頭件,不會(huì)存在重復(fù)包含的問(wèn)題),但是無(wú)法防止變量被重復(fù)定義。
# vi test.c
-------------------------------
#include
#include "test.h"
extern i;
extern void test1();
extern void test2();
int main()
{
test1();
printf("ok\n");
test2();
printf("%d\n",i);
return 0;
}
# vi test.h
-------------------------------
#ifndef _TEST_H_
#define _TEST_H_
char add1[] = "www.shellbox.cn\n";
char add2[] = "www.scriptbox.cn\n";
int i = 10;
void test1();
void test2();
#endif
# vi test1.c
-------------------------------
#include
#include "test.h"
extern char add1[];
void test1()
{
printf(add1);
}
# vi test2.c
-------------------------------
#include
#include "test.h"
extern char add2[];
extern i;
void test2()
{
printf(add2);
for (; i > 0; i--)
printf("%d-", i);
}
# Makefile
-------------------------------
test: test.o test1.o test2.o
test1.o: test1.c
test2.o: test2.c
clean:
rm test test.o test1.o test2.o
錯(cuò)誤:
test-1.0編譯后會(huì)出現(xiàn)"multiple definition of"錯(cuò)誤。
錯(cuò)誤分析:
由于工程中的每個(gè).c文件都是獨(dú)立的解釋的,即使頭文件有
#ifndef _TEST_H_
#define _TEST_H_
....
#enfif
在其他文件中只要包含了global.h就會(huì)獨(dú)立的解釋,然后每個(gè).c文件生成獨(dú)立的標(biāo)示符。在編譯器鏈接時(shí),就會(huì)將工程中所有的符號(hào)整合在一起,由于文件中有重名變量,于是就出現(xiàn)了重復(fù)定義的錯(cuò)誤。
解決方法
在.c文件中聲明變量,然后建一個(gè)頭文件(.h文件)在所有的變量聲明前加上extern,注意這里不要對(duì)變量進(jìn)行的初始化。然后在其他需要使用全局變量的.c文件中包含.h文件。編譯器會(huì)為.c生成目標(biāo)文件,然后鏈接時(shí),如果該.c文件使用了全局變量,鏈接器就會(huì)鏈接到此.c文件 。
test-2.0
# vi test.c
-------------------------------
#include
#include "test.h"
int i = 10;
char add1[] = "www.shellbox.cn\n";
char add2[] = "www.scriptbox.cn\n";
extern void test1();
extern void test2();
int main()
{
test1();
printf("ok\n");
test2();
printf("%d\n",i);
return 0;
}
# vi test.h
-------------------------------
#ifndef _TEST_H_
#define _TEST_H_
extern i;
extern char add1[];
extern char add2[];
void test1();
void test2();
#endif
# vi test1.c
-------------------------------
#include
#include "test.h"
void test1()
{
printf(add1);
}
# vi test2.c
-------------------------------
#include
#include "test.h"
void test2()
{
printf(add2);
for (; i > 0; i--)
printf("%d-", i);
}
聯(lián)系客服