找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3372|回复: 16

C/C++预处理指令#define,条件编译#ifdefine

 火... [复制链接]
  • 打卡等级:常驻代表
  • 打卡总天数:34
  • 打卡月天数:6
  • 打卡总奖励:9027
  • 最近打卡:2025-12-17 23:15:51

2823

主题

541

回帖

2万

积分

管理员

积分
22569
发表于 2021-4-18 17:20:51 | 显示全部楼层 |阅读模式
本文主要记录了C/C++预处理指令,常见的预处理指令如下:
#空指令,无任何效果
#include包含一个源代码文件
#define定义宏
#undef取消已定义的宏
#if如果给定条件为真,则编译下面代码
#ifdef如果宏已经定义,则编译下面代码
#ifndef如果宏没有定义,则编译下面代码
#elif如果前面的#if给定条件不为真,当前条件为真,则编译下面代码
#endif结束一个#if……#else条件编译块
#error停止编译并显示错误信息
条件编译命令最常见的形式为:
#ifdef 标识符
程序段1
#else
程序段2
#endif
例:#ifndef bool#define ture 1#define false 0#endif
在早期vc中bool变量用1,0表示,即可以这么定义,保证程序的兼容性
在头文件中使用#ifdef和#ifndef是非常重要的,可以防止双重定义的错误。
//main.cpp文件#include "cput.h"#include "put.h"int main(){ cput(); put(); cout << "Hello World!" << endl; return 0;}//cput.h 头文件#include <iostream>using namespace std;int cput(){ cout << "Hello World!" << endl; return 0;}//put.h头文件#include "cput.h"int put(){ cput(); return 0;}
编译出错;在main.cpp中两次包含了cput.h
尝试模拟还原编译过程;
当编译器编译main.cpp时
//预编译先将头文件展开加载到main.cpp文件中//展开#include "cput.h"内容#include <iostream>using namespace std;int cput(){ cout << "Hello World!" << endl; return 0;}//展开#include "put.h"内容//put.h包含了cput.h先展开#include <iostream>using namespace std;int cput(){ cout << "Hello World!" << endl; return 0;}int put(){ cput(); return 0;}int main(){ cput(); put(); cout << "Hello World!" << endl; return 0;}
很明显合并展开后的代码,定义了两次cput()函数;
如果将cput.h改成下面形式:
#ifndef _CPUT_H_#define _CPUT_H_#include <iostream>using namespace std;int cput(){ cout << "Hello World!" << endl; return 0;}#endif
当编译器编译main.cpp时合并后的main.cpp文件将会是这样的:
#ifndef _CPUT_H_#define _CPUT_H_#include <iostream>using namespace std;int cput(){ cout << "Hello World!" << endl; return 0;}#endif#ifndef _CPUT_H_#define _CPUT_H_#include <iostream>using namespace std;int cput(){ cout << "Hello World!" << endl; return 0;}#endifint put(){ cput(); return 0;}int main(){ cput(); put(); cout << "Hello World!" << endl; return 0;}
这次编译通过运行成功;因为在展开put.h中包含的cput.h,会不生效,前面已经定义了宏_CPUT_H_

工控课堂 www.gkket.com
  • 打卡等级:即来则安
  • 打卡总天数:29
  • 打卡月天数:1
  • 打卡总奖励:7791
  • 最近打卡:2025-12-13 17:25:16

2540

主题

1355

回帖

2万

积分

管理员

积分
21304
发表于 2021-4-18 17:20:52 | 显示全部楼层
看了楼主的帖子,不由得精神一振,豁然开朗,牛掰
工控课堂 www.gkket.com

0

主题

119

回帖

384

积分

注册会员

积分
384
发表于 2021-4-20 14:50:54 | 显示全部楼层
感谢分享,佩服佩服!
工控课堂 www.gkket.com

0

主题

163

回帖

341

积分

注册会员

积分
341
发表于 2021-7-17 22:26:33 | 显示全部楼层
bshdhdhdjdjxjdbdjek
工控课堂 www.gkket.com

0

主题

82

回帖

128

积分

新手上路

积分
128
发表于 2025-11-12 21:16:04 | 显示全部楼层
楼主辛苦啦,期待下一篇分享
工控课堂 www.gkket.com

0

主题

99

回帖

155

积分

新手上路

积分
155
发表于 2025-11-12 22:58:48 | 显示全部楼层
这逻辑绝了,分析得太到位了吧
工控课堂 www.gkket.com

0

主题

116

回帖

422

积分

注册会员

积分
422
发表于 2025-11-12 23:10:09 | 显示全部楼层
理性围观,感觉大家说的都有道理~
工控课堂 www.gkket.com

0

主题

99

回帖

169

积分

新手上路

积分
169
发表于 2025-11-12 23:17:38 | 显示全部楼层
这评论区卧虎藏龙,个个都是人才!
工控课堂 www.gkket.com

0

主题

108

回帖

178

积分

新手上路

积分
178
发表于 2025-11-12 23:19:44 | 显示全部楼层
已转发给朋友,一起感受这份快乐~
工控课堂 www.gkket.com

0

主题

97

回帖

361

积分

注册会员

积分
361
发表于 2025-11-12 23:26:52 | 显示全部楼层
来凑个热闹,为楼主增加点人气!
工控课堂 www.gkket.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|手机版|免责声明|本站介绍|工控课堂 ( 沪ICP备20008691号-1 )

GMT+8, 2025-12-23 07:36 , Processed in 0.084605 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表