有3个文件分别是text1.h text2.h text.c
text1.h
int a=1;
text2.h
#include "text1.h"
int b = 2;
text.c
#include "text1.h"
#include "text2.h"
#include <iotaream>
int main()
{
return 0;
}//报错 会提示a重复定义
以上text.c可以翻译为如下
int a=1;//#include "text1.h"
int b=2;//#include "text2.h"
int a=1;
#include <iotaream>
int main()
{
return 0;
}
头文件中常用到
#ifdef, ifndef 条件编译
#ifndef 标识符 //当标识符没有定义过(没有用#define定义过),则对程序进行编译
//程序段
#endif
以上text1.h与text2.h可以改写为如下
#ifdef __text1__
#ifndef __text1__
int a=1;
#endif
#ifdef __text2__
#ifndef __text2__
int b=2;
#endif
这样#inlcude就没问题了,但是通常标识符会后面会加个h__texe1__H_