zoukankan      html  css  js  c++  java
  • C语言-条件编译使用分析

    1、基本概念

      条件编译的行为类似于C语言中的if…else… 

      条件编译是预编译指示命令,用于控制是否编译某段代码

    2、实例分析

    条件编译初探     22-1.c

     1 #include <stdio.h>  
     2   
     3 #define C 1  
     4   
     5 int main()  
     6 {  
     7     const char* s;  
     8   
     9     #if( C == 1 )  
    10         s = "This is first printf...
    ";  
    11     #else  
    12         s = "This is second printf...
    ";  
    13     #endif  
    14   
    15     printf("%s", s);  
    16       
    17     return 0;  
    18 }  

    3、条件编译的本质

      预编译器根据条件编译指令有选择的删除代码 

      编译器不知道代码分支的存在 

      if. .. else ... 语句在运行期进行分支判断 

      条件编译指令在预编译期进行分支判断 

      可以通过命令行定义宏 

                   -  gcc -Dmacro=value file.c 或 gcc -Dmacro file.c

    4、#include的本质

      #include的本质是将已经存在的文件内容嵌入到当前文件中 

      #include的间接包含同样会产生嵌入文件内容的操作

    如何解决间接包含同—个头文件产生编译错误?

    解决方案:

    1 #ifndef _HEADER_FILE H   
    2   
    3 #define _HEADER_FILE H   
    4   
    5   // source code   
    6   
    7 #endif   

    5、实例分析

    条件编译的使用    

    global.h

    1 // global.h  
    2 #ifndef _GLOBAL_H_  
    3 #define _GLOBAL_H_  
    4 int global = 10;  
    5   
    6 #endif  

    test.h

     1 // test.h  
     2   
     3 #ifndef _TEST_H_  
     4 #define _TEST_H_  
     5 #include "global.h"  
     6   
     7 const char* NAME = "test.h";  
     8 char* hello_world()  
     9 {      
    10     return "Hello world!
    ";  
    11 }  
    12   
    13 #endif  

    22-3.cpp

     1 //#include <stdio.h>  
     2 #include "test.h"  
     3 #include "global.h"  
     4   
     5 int main()  
     6 {  
     7     const char* s = hello_world();  
     8     int g = global;  
     9       
    10     // printf("%s
    ", NAME);  
    11     // printf("%d
    ", g);  
    12       
    13     return 0;  
    14 }  

    条件编译可以解决头文件重复包含的编译错误 

    7、条件编译的意义 

      条件编译使得我们可以按不同的条件编译不同的代码段,因而可以产生不同的目标代码 

      #if…#else…#endif被预编译器处理,而if…else .. 语句被编译器处理,必然被编译进目标代码 

    实际工程中条件编译主要用于以下两种情况: 

          -不同的产品线共用一份代码 

          -区分编译产品的调试版和发布版

    8、实例分析

    产品线区分及调试代码应用     product.h     22-4.c

    product.h

    1 #define DEBUG 1  //调试版
    2 #define HIGH  1  //高端产品

    22-4.c

     1 #include <stdio.h>  
     2 #include "product.h"  
     3   
     4 #if DEBUG  
     5     #define LOG(s) printf("[%s:%d] %s
    ", __FILE__, __LINE__, s)  
     6 #else  
     7     #define LOG(s) NULL  
     8 #endif  
     9   
    10 #if HIGH  
    11 void f()  
    12 {  
    13     printf("This is the high level product!
    ");  
    14 }  
    15 #else  
    16 void f()  
    17 {  
    18 }  
    19 #endif  
    20   
    21 int main()  
    22 {  
    23     LOG("Enter main() ...");  
    24       
    25     f();  
    26       
    27     printf("1. Query Information.
    ");  
    28     printf("2. Record Information.
    ");  
    29     printf("3. Delete Information.
    ");  
    30       
    31     #if HIGH  
    32     printf("4. High Level Query.
    ");  
    33     printf("5. Mannul Service.
    ");  
    34     printf("6. Exit.
    ");  
    35     #else  
    36     printf("4. Exit.
    ");  
    37     #endif  
    38       
    39     LOG("Exit main() ...");  
    40       
    41     return 0;  
    42 }  

    9、小结

            通过编译器命令行能够定义预处理器使用的宏 

            条件编译可以避免围复包含头同—个头文件 

            条件编译是在工程开发中可以区别不同产品线的代码 

            条件编译可以定义产品的发布版和调试版

  • 相关阅读:
    centos7手动搭建redis集群
    Xshell突破四个窗口限制
    Redis官方集群规范
    Redis官方集群教程
    centos7 更新阿里YUM源
    gitlab配置ssh
    Java前端控制器模式~
    Java数据访问对象模式
    Java组合实体模式~
    Java业务代理模式~
  • 原文地址:https://www.cnblogs.com/lemaden/p/10127826.html
Copyright © 2011-2022 走看看