`

#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 ...

    Tips and tricks using the preprocessor 2

    在大多数情况下,如果只需要检查宏是否被定义,那么使用 `#ifdef` 或 `#ifndef` 就足够了。但如果需要进行更复杂的条件判断,那么 `#if` 更为合适。 通过这篇文章的学习,希望你能更好地理解预处理器的工作原理,并...

    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. ...

    在非MFC程序中引用CString

    #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers #include <afx.h> // MFC Header Files #include <windows.h> // Windows Header Files #include #include #include #...

    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 ...

    A fast preprocessor for C and C++.zip

    2. **条件编译**:预处理器支持条件编译指令,比如`#if`, `#ifdef`, `#ifndef`, `#else`, `#elif`和`#endif`,这使得代码可以根据不同的编译环境或配置选择性地编译部分代码。优化可能涉及更快地处理这些条件语句。 ...

    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) ...

    Tips and tricks using the preprocessor 1

    这通常通过`#ifdef`、`#ifndef`、`#if` 等预处理指令实现。例如,以下代码展示了如何使用条件编译来避免重复定义: ```c #ifndef MAX_LENGTH #define MAX_LENGTH 100 #endif ``` 这段代码中,如果`MAX_LENGTH`已经...

    一机一码网络认证加密 源代码 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++ Tips and Traps

    使用预处理器的`#ifdef`、`#ifndef`和`#define`指令来控制文件的包含,以及防止头文件的重复包含,避免重复定义的问题。例如: ```cpp #ifndef FOOBAR_H #define FOOBAR_H // 头文件内容 #endif ``` ### 9. 引用...

    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 ...

    设计java程序与c语言接口

    #ifndef _Included_NativeHello #define _Included_NativeHello #ifdef __cplusplus extern "C" { #endif /* * Class: NativeHello * Method: nativeHelloWorld * Signature: ()V */ JNIEXPORT void JNICALL ...

    jni返回对象数组例子

    // getters and setters... } ``` 接下来,我们需要创建一个C/C++头文件,这个文件包含了JNI函数的原型。这个头文件通常由`javah`工具自动生成,基于我们的Java类: ```c #include #ifndef _Included_HelloJNI ...

Global site tag (gtag.js) - Google Analytics