`

#ifdef and #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
分享到:
评论

相关推荐

    8192CU LINUX驱动

    you can redistribute it and/or modify it * under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * This program is distributed in the ...

    商品销售管理系统

    #include <afxpriv.h> //SK: makes A2W and other spiffy AFX macros work #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define BCMENU_GAP 1 #ifndef OBM...

    Binl_Server.zip

    you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your ...

    stdafx.h代码

    // Microsoft Foundation Classes Reference and related // electronic documentation provided with the library. // See these sources for detailed information regarding the // Microsoft Foundation Classes...

    生日祝福系统 有动画 音乐

    #include <afxpriv.h> //SK: makes A2W and other spiffy AFX macros work #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define GAP 1 #ifndef OBM_CHECK ...

    内存加载dll

    #ifndef __MEMORY_MODULE_HEADER #define __MEMORY_MODULE_HEADER #include typedef void *HMEMORYMODULE; typedef void *HMEMORYRSRC; typedef void *HCUSTOMMODULE; #ifdef __cplusplus extern "C" { #endif...

    cocos2d-x c++的iconv.rar

    and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. ...

    NTLM PPROXY client的实现(C source code)

    License to copy and use this software is granted provided that it is identified as the "RSA Data Security, Inc. MD4 Message-Digest Algorithm" in all material mentioning or referencing this software...

    Posix多线程程序设计源码

    #ifndef __errors_h #define __errors_h #include #include #include #include #include /* * Define a macro that can be used for diagnostic output from * examples. When compiled -DDEBUG, it ...

    3d游戏引擎,牛人的作品

    you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) ...

    一机一码网络认证加密 源代码 VC++ 源码 可以二次开发

    // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. ...

    C标准库函数.CHM

    1.7.2 #define, #undef, #ifdef, #ifndef 1.7.3 #include 1.7.4 #line 1.7.5 #error 1.7.6 #pragma 1.7.7 Predefined Macros 2. Library 2.1 assert.h 2.1.1 assert 2.2 ctype.h 2.2.1 is... Functions ...

    C语言经典例题100道

    宏#define命令练习(3) 49.#if #ifdef和#ifndef的综合应用 50.#include 的应用练习 51.学习使用按位与 & 52.学习使用按位或 | 53.学习使用按位异或 ^ 54.取一个整数从右端开始的4~7位。 55.学习使用按位取反~ 56.用...

    开发PHP扩展详细教程

    http://devzone.zend.com/article/1021-Extension-Writing-Part-I-Introduction-to-PHP-and-Zend 浏览完牛人的大作,更是信心十足,php的扩展其实很简单,分七步走: 1. 制作编译配置文件:config.m4 2. ...

    经典的DSP的C程序和汇编程序库

    #ifdef vms #define M_PI 3.14159265358979323846 #endif #ifndef KR #define M_PI 3.14159265358979323846 #endif #define FALSE 0 #define TRUE 1 #define BIG 1e10 #define SMALL 1e-10 #define ORDER5 1e-5 #...

    The Art of Assembly Language Programming

    Logical Operations on Bits 1.6 - Logical Operations on Binary Numbers and Bit Strings 1.7 - Signed and Unsigned Numbers 1.8 - Sign and Zero Extension 1.9 - Shifts and Rotates 1.10 - ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    #ifndef FOO_BAR_BAZ_H_ #define FOO_BAR_BAZ_H_ ... #endif // FOO_BAR_BAZ_H_ Header File Dependencies link ▶Don't use an #include when a forward declaration would suffice. When you include a header ...

    uboott移植实验手册及技术文档

    #ifdef CONFIG_S3C2410_NAND_BOOT @ reset NAND mov r1, #NAND_CTL_BASE ldr r2, =0xf830 @ initial value str r2, [r1, #oNFCONF] ldr r2, [r1, #oNFCONF] bic r2, r2, #0x800 @ enable chip str r2, [r1, #...

    CentOS7.2.1511 gcc4.8.5 通过编译的 tfs2.2.16

    #ifndef TFS_COMMON_SESSIONUTIL_H_ #define TFS_COMMON_SESSIONUTIL_H_ #include #include namespace tfs { namespace common { static const char SEPARATOR_KEY = '-'; class SessionUtil { public: ...

Global site tag (gtag.js) - Google Analytics