zoukankan      html  css  js  c++  java
  • c++ #ifdef的用法

    http://www.tuicool.com/articles/mIJnumB

    #ifdef的用法

       灵活使用#ifdef指示符,我们可以区隔一些与特定头文件、程序库和其他文件版本有关的代码。
    代码举例:新建define.cpp文件

    [cpp] view plaincopy
     
    1. #include "iostream.h"  
    2. int main()  
    3. {  
    4. #ifdef DEBUG   
    5. cout<< "Beginning execution of main()";  
    6. #endif   
    7. return 0;  
    8. }  
    运行结果为:Press any key to continue

    改写代码如下:
    [cpp] view plaincopy
     
    1. #include "iostream.h"  
    2. #define DEBUG  
    3. int main()  
    4. {  
    5. #ifdef DEBUG   
    6. cout<< "Beginning execution of main()";  
    7. #endif   
    8. return 0;  
    9. }  
    运行结果为:Beginning execution of main()
    Press any key to continue

    更一般的情况是,#define语句是包含在一个特定的头文件中。
    比如,新建头文件head.h,在文件中加入代码:

    [cpp] view plaincopy
     
    1. #ifndef DEBUG  
    2. #define DEBUG  
    3. #endif  
    4.   
    5. 而在define.cpp源文件中,代码修改如下:  
    6. #include "iostream.h"  
    7. #include "head.h"  
    8. int main(){  
    9. #ifdef DEBUG   
    10. cout<< "Beginning execution of main()";  
    11. #endif   
    12. return 0;  
    13. }  

    运行结果如下:Beginning execution of main()
    Press any key to continue
    结论:通过使用#ifdef指示符,我们可以区隔一些与特定头文件、程序库和其他文件版本有关的代码

     

    #if, #ifdef, #ifndef, #else, #elif, #endif的用法:

      这些命令可以让编译器进行简单的逻辑控制,当一个文件被编译时,你可以用这些命令去决定某些代码的去留,

    这些命令式条件编译的命令。

    常见的条件编译的三种形式:

    ①第一种形式:  
    #if defined(或者是ifdef)<标识符(条件)> 

    <程序段1>

    #endif  
    ②第二种形式:  
    #if !defined(或者是ifndef)<标识符(条件)> 

    <程序段1> 

      #ifdef … 

    [#elif … ] 

    [#elif …] 

    #else …  

    #endif

    示例:

    #include <iostream>

    using namespace std;

    int main() 

    #if DEBUG  /*或者是#ifdef DEBUG*/ 
    cout << "条件成立,DEBUG已经定义了!" <<endl; 
    #else 
    cout << "条件不成立,DEBUG还没定义" <<endl; 
    #endif 
    return 0; 
    }

    //结果输出: 条件不成立,DEBUG还没定义

    //如果是添加了#define DEBUG ,输出结果是:条件成立,DEBUG已经定义了!

    #include <iostream> 
    using namespace std; 
    #define DEBUG 
    int main() 

    #ifdef DEBUG /*或者是#ifdef DEBUG*/ 
    cout << "条件成立,DEBUG已经定义了!" <<endl; 
    #else 
    cout << "条件不成立,DEBUG还没定义" <<endl; 
    #endif 
    return 0; 
    }

    //要注意的是,如果是#define 宏名,没有宏体如 #define DEBUG,就必须使用#ifdef或#ifndef与之对应,

    //如果是#define 宏名 宏体,如 #define NUM 1,#if 和#ifdef都可以使用。

    /*

    #define的用法:

    */

    示例二:

    #include <iostream> 

    using namespace std; 
    #define NUM  10 
    int main() 

            #ifndef NUM 
            cout << "NUM没有定义!"<<endl; 
            #elif NUM >= 100 
            cout << "NUM >100" <<endl; 
            #elif NUM <100 && NUM >10 
            cout << "10 < NUM < 100" <<endl; 
            #elif NUM == 10 
            cout << "NUM ==10" <<endl; 
            #else 
            cout << "NUM < 10" << endl; 
            #endif 
            return 0; 

    //输出NUM ==10 

     

     

     

    也可以在mk文件定义NUM

    ifeq ($(BOARD_SCREENRECORD_LANDSCAPE_ONLY),true)
    LOCAL_CFLAGS += -DNUM
    endif

     

     

     

     

  • 相关阅读:
    3 Redis 的常用五大数据类型
    Oracle12C 基本操作和使用
    CentOS7安装VNC服务
    CentOS7.6 安装Oracle12C(下)
    CentOS7.6 安装Oracle12C(上)
    博主创建了一个AGV吧的Discuz,欢迎各位加入进来
    六、openTCS4.17汉化版源码包含通信DEMO,gitee地址见内容
    毕马威图形数独
    五、OpenTCS4.12的模拟运行
    四、通过Socket实现跟AGV小车通信(仅做Demo演示,跟实际工厂运行无关)
  • 原文地址:https://www.cnblogs.com/wanqieddy/p/4377937.html
Copyright © 2011-2022 走看看