在寫C代碼的時候,經常需要在頭文件中包含有些預定義的信息。比如一些結構體的聲明,外部變量,函數的聲明等。
有時候覺得有必要在頭文件中定義一些變量,這樣在源文件中就不需要定義了。但是這樣做,有很大的問題。
比如
[cpp] view plain copy
//test.h
1 #ifndef _TEST_H
2 #define _TEST_H
3 int x;
4 const char *name = “hhcn”;
5 #endif
~
[cpp] view plain copy
//test.c
#include “test.h”
[cpp] view plain copy
//main.c
1 #include