頭文件重複引用

#include文件的一個不利之處在於一個頭文件可能會被多次包含,爲了說明這種錯誤,考慮下面的代碼:

#include "x.h"
#include "x.h"

顯然,這裏文件x.h被包含了兩次,沒有人會故意編寫這樣的代碼。但是下面的代碼:
#include "a.h"
#include "b.h"

看上去沒什麼問題。如果a.h和b.h都包含了一個頭文件x.h。那麼x.h在此也同樣被包含了兩次,只不過它的形式不是那麼明顯而已。

多重包含在絕大多數情況下出現在大型程序中,它往往需要使用很多頭文件,因此要發現重複包含並不容易。要解決這個問題,我們可以使用條件編譯。如果所有的頭文件都像下面這樣編寫:
#ifndef _HEADERNAME_H
#define _HEADERNAME_H

...

#endif

那麼多重包含的危險就被消除了。當頭文件第一次被包含時,它被正常處理,符號_HEADERNAME_H被定義爲1。如果頭文件被再次包含,通過條件編譯,它的內容被忽略。符號_HEADERNAME_H按照被包含頭文件的文件名進行取名,以避免由於其他頭文件使用相同的符號而引起的衝突。

但是,你必須記住預處理器仍將整個頭文件讀入,即使這個頭文件所有內容將被忽略。由於這種處理將託慢編譯速度,所以如果可能,應該避免出現多重包含。

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~我是分隔符~~~~~~~~~~~~~~~~~~~~~~~~~

 使用#ifndef只是防止了頭文件被重複包含(其實本例中只有一個頭件,不會存在重複包含的問題),但是無法防止變量被重複定義

# vi test.c
-------------------------------
#include <stdio.h>
#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[] = "char1";
char add2[] = "char2";
int i = 10;
void test1();
void test2();

#endif



# vi test1.c
-------------------------------
#include <stdio.h>
#include "test.h"

extern char add1[];

void test1()
{
   printf(add1);
}



# vi test2.c
-------------------------------
#include <stdio.h>
#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


錯誤:
test-1.0編譯後會出現"multiple definition of"錯誤。

錯誤分析:
由於工程中的每個.c文件都是獨立的解釋的,即使頭文件有
#ifndef _TEST_H_
#define _TEST_H_
....
#enfif
在其他文件中只要包含了global.h就會獨立的解釋,然後每個.c文件生成獨立的標示符。在編譯器鏈接時,就會將工程中所有的符號整合在一起,由於文件中有重名變量,於是就出現了重複定義的錯誤。

解決方法
.c文件中聲明變量,然後建一個頭文件(.h文件)在所有的變量聲明前加上extern,注意這裏不要對變量進行的初始化。然後在其他需要使用全局變量的.c文件中包含.h文件。編譯器會爲.c生成目標文件,然後鏈接時,如果該.c文件使用了全局變量,鏈接器就會鏈接到此.c文件 。






test-2.0

# vi test.c
-------------------------------
#include <stdio.h>
#include "test.h"

int i = 10;
char add1[] = "char1";
char add2[] = "char2";
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 <stdio.h>
#include "test.h"

void test1()
{
   printf(add1);
}


# vi test2.c
-------------------------------
#include <stdio.h>
#include "test.h"

void test2()
{
   printf(add2);
   for (; i > 0; i--)
       printf("%d-", i);
}

發佈了14 篇原創文章 · 獲贊 19 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章