条件编译的一个作用
来源:互联网 发布:手机淘宝分期付款流程 编辑:程序博客网 时间:2024/05/17 06:30
条件编译的一个作用
条件编译的一个作用是用于处理文件之间的关系。使其在同一个文件中不被重复包含。
如果文件的关系是这样:
A
/ \
C D
\ /
B
上面的文件有关系,C中包含A,D中包含A。 B中要包括C和D,这样条件编译就可把在预处理阶段去掉重复包含文件A。
$ cattest1.h
#ifndef__A__H__
#define__A__H__
int i =0
voidmyprint();
#endif
chenwl@jftest$~
$ cattest3.h
#ifndef__TEAT3__H__
#define__TEAT3__H__
#include"test1.h"
#endif
chenwl@jftest$~
$ cattest4.h
#ifndef__TEST4__H__
#define__TEST4__H__
#include"test1.h"
#endif
Test3.c文件:
#include<stdio.h>
#include"test3.h"
#include"test4.h"
intmain(){
printf("%d\n",i);
printf("hello world\n");
}
看看gcc -o test3.E -E test3.c 预处理的结果
# 2"test3.c" 2
# 1"test3.h" 1
# 1"test1.h" 1
int i=0;
voidmyprint();
# 4"test3.h" 2
# 3"test3.c" 2
# 1"test4.h" 1
# 4"test3.c" 2
intmain(){
printf("%d\n",i);
printf("hello world\n");
}
编译通过,int I =0和voidmyprint()只出现一次。
把test1.h文件中的条件编译去掉。
//#ifndef__A__H__
//#define__A__H__
int i=0;
voidmyprint();
//#endif
编译报错:
test1.h:3:5:error: redefinition of 'i'
test1.h:3:5:note: previous definition of 'i' was here
再看gcc -o test3.E -E test3.c结果:
有两个int i =0; void myprint(); 重复定义了。
# 2"test3.c" 2
# 1"test3.h" 1
# 1"test1.h" 1
int i =0;
void myprint();
# 4"test3.h" 2
# 3"test3.c" 2
# 1"test4.h" 1
# 1"test1.h" 1
int i=0;
voidmyprint();
# 4"test4.h" 2
# 4"test3.c" 2
intmain(){
printf("%d\n",i);
printf("hello world\n");
}
- 条件编译的一个作用
- 条件编译的作用
- 条件编译的作用以及原因
- [C++]条件编译——#if #ifdef 的作用
- 简单的条件编译
- 条件编译的用途
- 条件编译的认识
- 条件编译的用法
- C++的条件编译
- c++的条件编译
- 条件编译的使用
- 一个qt的例子说明QWaitCondition的作用(条件变量)
- 多个条件的条件编译
- 条件编译 ifndef/define/endif作用和用法
- c++--------------条件编译的问题
- C/C++的条件编译
- 条件编译宏的使用
- C/C++的条件编译
- 大话设计模式之抽象工厂模式
- 使用Uniread实现SQLplus翻页功能
- hdu 1392 Surround the Trees
- log4j配置深度详解
- 给大家推荐几个能赚钱的网站
- 条件编译的一个作用
- 开始学习android开发
- 2013-08-11 Hello word|异常|代码书写规范|编译器虚拟机|注释|命名规则|数据类型|不常用的运算符|语法|
- ntpq –p命令查看连接的NTP服务器
- java异常处理
- FZOJ2110 star(DFS)
- Expecting a string, date or number here, Expression x is instead a freemarker.template.TemplateBoole
- 01.UTinventory多用户进销存系统概述
- java文件读取