#ifdef and #ifndef

预处理器使用详解
本文介绍了预处理器命令 #ifdef 和 #ifndef 的用法,包括如何防止头文件被重复包含以及如何根据定义的存在与否来开关调试代码。

Preprocessor: #ifdef and #ifndef

The #ifdef (if defined) and #ifndef (if not defined) preprocessor commands are used to test if a preprocessor variable has been "defined". There are two common uses for this, with slightly different patterns.

Prevent multiple definitions in header files

When there definitions in a header file that can not be made twice, the code below should be used. A header file may be included twice other include files include it, or an included file includes it and the source file includes it again.

To prevent bad effects from a double include, it is common to surround the body in the include file with the following (where MYHEADER_H is replaced by a name that is appropriate for your program).

#ifndef MYHEADER_H
#define MYHEADER_H
. . .	// This will be seen by the compiler only once 
#endif /* MYHEADER_H */

Turning debugging code off and on

Debugging code is necessary in programs, however, it is not usually appropriate to leave it in the delivered code. The preprocessor #ifdef commmand can surround the debugging code. If DEBUG is defined as below (probably in an include file) all debugging statement surrounded by the #ifdef DEBUG statement will be active. However, if it isn't defined, none of the statements will make it thru the preprocessor.

#define DEBUG
. . .
#ifdef DEBUG
  . . . // debugging output
#endif
boot/src/lib_tapo_nand/tapo_nand_upgrade.c:259:#ifdef USE_NAND_FLASH boot/src/lib_tapo_nand/tapo_nand_upgrade.c:290:#ifdef USE_NAND_FLASH boot/src/lib_tapo_nand/tapo_nand_upgrade.c:341:#ifdef USE_NAND_FLASH boot/src/lib_tapo_nand/tapo_nand_upgrade.c:623:#ifdef USE_NAND_FLASH boot/src/lib_verify/validateFirmwareWithRecover.c:82:#ifdef USE_NAND_FLASH boot/src/lib_verify/validateFirmwareWithRecover.c:208:#ifdef USE_NAND_FLASH boot/src/lib_verify/validateFirmwareWithRecover.c:239:#ifdef USE_NAND_FLASH boot/src/lib_verify/validateFirmwareWithRecover.c:290:#ifdef USE_NAND_FLASH boot/src/lib_verify/validateFirmwareWithRecover.c:321:#ifdef USE_NAND_FLASH boot/src/lib_verify/validateFirmwareWithRecover.c:434:#ifdef USE_NAND_FLASH boot/src/lib_verify/validateFirmwareWithRecover.c:504:#ifndef USE_NAND_FLASH boot/src/lib_verify/validateFirmwareWithRecover.c:515:#ifndef USE_NAND_FLASH /* It is not true for nand, because there are gaps in kernel and romFS partitions */ boot/src/lib_verify/validateFirmwareWithRecover.c:619:#ifdef USE_NAND_FLASH boot/src/lib_verify/validateFirmwareWithRecover.c:670:#ifdef USE_NAND_FLASH boot/src/lib_verify/validateFirmwareWithRecover.c:690:#ifdef USE_NAND_FLASH boot/src/lib_verify/validateFirmwareWithRecover.c:734:#ifdef USE_NAND_FLASH boot/src/lib_verify/validateFirmwareWithRecover.c:780:#ifdef USE_NAND_FLASH boot/src/lib_verify/validateFirmwareWithRecover.c:796:#ifdef USE_NAND_FLASH boot/src/common/Makefile:283:obj-$(CONFIG_USE_NAND_FLASH) += cmd_auto_upgrade.o boot/src/common/Makefile:284:obj-$(CONFIG_USE_NAND_FLASH) += cmd_mstar.o cmd_osd.o boot/src/Makefile:490:ifeq ($(CONFIG_USE_NAND_FLASH),y) boot/src/Makefile:491:KBUILD_CFLAGS+=-DUSE_NAND_FLASH boot/src/Makefile:970:libs-$(CONFIG_USE_NAND_FLASH) += fs/ boot/src/Makefile:972:libs-$(CONFIG_USE_NAND_FLASH) += disk/ boot/src/Makefile:977:libs-$(CONFIG_USE_NAND_FLASH) += drivers/mmc/ boot/src/Makefile:1203:ifdef CONFIG_USE_NAND_FLASH boot/src/lib_uip/apps/user_config/user_config.c:439:#ifdef USE_NAND_FLASH boot/src/lib_uip/apps/user_config/user_config.c:472:#ifdef USE_NAND_FLASH boot/src/lib_uip/apps/upgrade/http_upgrade.c:1137:#ifdef USE_NAND_FLASH boot/src/lib_uip/apps/upgrade/http_upgrade.c:1398:#ifdef USE_NAND_FLASH boot/src/lib_uip/apps/upgrade/http_upgrade.c:1457:#endif /* USE_NAND_FLASH */ boot/src/lib_uip/apps/upgrade/http_upgrade.c:1494:#ifdef USE_NAND_FLASH boot/src/lib_uip/apps/upgrade/http_upgrade.c:1850:#ifdef USE_NAND_FLASH boot/src/lib_uip/apps/upgrade/http_upgrade.c:1894:#ifdef USE_NAND_FLASH boot/src/lib_uip/apps/upgrade/http_upgrade.c:2008:#ifdef USE_NAND_FLASH boot/src/lib_uip/apps/upgrade/http_upgrade.c:2039:#ifdef USE_NAND_FLASH boot/src/lib_uip/uip_drv.c:85:#ifdef USE_NAND_FLASH boot/src/lib_uip/uip_drv.c:130:#ifdef USE_NAND_FLASH boot/src/lib_uip/uip_drv.c:181:#ifdef USE_NAND_FLASH boot/configs/dbg_normalboot.mk:118:ifdef CONFIG_USE_NAND_FLASH boot/configs/dbg_normalboot.mk:119:DBG_NORMALBOOT_MAKEOPTS+=CONFIG_USE_NAND_FLASH=y boot/configs/normalboot.mk:118:ifdef CONFIG_USE_NAND_FLASH boot/configs/normalboot.mk:119:NORMALBOOT_MAKEOPTS+=CONFIG_USE_NAND_FLASH=y boot/configs/factoryboot.mk:161:ifdef CONFIG_USE_NAND_FLASH boot/configs/factoryboot.mk:162:FACTORYBOOT_MAKEOPTS+=CONFIG_USE_NAND_FLASH=y boot/configs/factoryboot.mk:163:export USE_NAND_FLASH := $(CONFIG_USE_NAND_FLASH) boot/configs/compile.mk:6:ifdef CONFIG_USE_NAND_FLASH boot-ssc375/src/lib_tapo_nand/tapo_nand_upgrade.c:259:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_tapo_nand/tapo_nand_upgrade.c:290:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_tapo_nand/tapo_nand_upgrade.c:341:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_tapo_nand/tapo_nand_upgrade.c:623:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_verify/validateFirmwareWithRecover.c:80:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_verify/validateFirmwareWithRecover.c:199:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_verify/validateFirmwareWithRecover.c:230:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_verify/validateFirmwareWithRecover.c:281:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_verify/validateFirmwareWithRecover.c:312:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_verify/validateFirmwareWithRecover.c:396:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_verify/validateFirmwareWithRecover.c:464:#ifndef USE_NAND_FLASH boot-ssc375/src/lib_verify/validateFirmwareWithRecover.c:475:#ifndef USE_NAND_FLASH /* It is not true for nand, because there are gaps in kernel and romFS partitions */ boot-ssc375/src/lib_verify/validateFirmwareWithRecover.c:496:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_verify/validateFirmwareWithRecover.c:547:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_verify/validateFirmwareWithRecover.c:567:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_verify/validateFirmwareWithRecover.c:608:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_verify/validateFirmwareWithRecover.c:654:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_verify/validateFirmwareWithRecover.c:670:#ifdef USE_NAND_FLASH boot-ssc375/src/Makefile:572:ifeq ($(CONFIG_USE_NAND_FLASH),y) boot-ssc375/src/Makefile:573:KBUILD_CFLAGS+=-DUSE_NAND_FLASH boot-ssc375/src/Makefile:1188:libs-$(CONFIG_USE_NAND_FLASH) += fs/ boot-ssc375/src/Makefile:1190:libs-$(CONFIG_USE_NAND_FLASH) += disk/ boot-ssc375/src/lib_uip/apps/user_config/user_config.c:439:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_uip/apps/user_config/user_config.c:472:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_uip/apps/upgrade/http_upgrade.c:1137:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_uip/apps/upgrade/http_upgrade.c:1398:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_uip/apps/upgrade/http_upgrade.c:1457:#endif /* USE_NAND_FLASH */ boot-ssc375/src/lib_uip/apps/upgrade/http_upgrade.c:1494:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_uip/apps/upgrade/http_upgrade.c:1850:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_uip/apps/upgrade/http_upgrade.c:1894:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_uip/apps/upgrade/http_upgrade.c:2008:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_uip/apps/upgrade/http_upgrade.c:2039:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_uip/uip_drv.c:85:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_uip/uip_drv.c:130:#ifdef USE_NAND_FLASH boot-ssc375/src/lib_uip/uip_drv.c:181:#ifdef USE_NAND_FLASH boot-ssc375/configs/dbg_normalboot.mk:123:ifdef CONFIG_USE_NAND_FLASH boot-ssc375/configs/dbg_normalboot.mk:124:DBG_NORMALBOOT_MAKEOPTS+=CONFIG_USE_NAND_FLASH=y boot-ssc375/configs/normalboot.mk:123:ifdef CONFIG_USE_NAND_FLASH boot-ssc375/configs/normalboot.mk:124:NORMALBOOT_MAKEOPTS+=CONFIG_USE_NAND_FLASH=y boot-ssc375/configs/factoryboot.mk:165:ifdef CONFIG_USE_NAND_FLASH boot-ssc375/configs/factoryboot.mk:166:FACTORYBOOT_MAKEOPTS+=CONFIG_USE_NAND_FLASH=y boot-ssc375/configs/factoryboot.mk:167:export USE_NAND_FLASH := $(CONFIG_USE_NAND_FLASH) boot-ssc375/configs/compile.mk:6:ifdef CONFIG_USE_NAND_FLASH
最新发布
09-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值