C语言进阶剖析 22 条件编译使用分析

基本概念

  • 条件编译的行为类似于 C 语言中的 if...else...
  • 条件编译是预处理指示命令,用于控制是否编译某段代码

示例分析: 条件编译初探

test.c

#include <stdio.h>

#define C 1

int main()
{
	const char  *s;
	#if( C == 1 )
		s = " This if first printf...\n";
	#else 
		s = "This is second printf...\n";
	#endif
	printf("%s.\n",s);
	return 0;
}
输出:
This is first printf...    

test.i

    int main()
    {
      const char* s;
      s = "This is first printf...\n";
      return 0;
    }

条件编译的本质

  • 预编译器根据条件编译指令有选择的删除代码
  • 编译器不知道代码分支的存在
  • if…else…语句在运行期进行判断
  • 条件编译指令在预编译期进行分支判断
  • 可以通过命令行定义宏
        ○ gcc -Dmacro=value file.c
        ○ gcc -Dmacro file.c
        ■ gcc -DC test.c
        ■ gcc -DC=1 test.c

#include的本质

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

  • #include 的间接包含同样会产生嵌入文件内容的操作
    在这里插入图片描述

  • 问题间接包含同一个头文件是否会产生编译错误呢?

示例分析: 条件编译的使用

test.c

#include "test.h"
#include "global.h"

int main()
{
    const char* s = hello_word();
    int g = global;
    
    return 0;
}

test.h


#include "global.h"

const char* Name = "test.h";

char* hello_word()
{
    return "Hello world\n";
}

global.h

int global = 10;
输出:
In file included from test.c:2:
global.h:1: error: redefinition of ‘global’
global.h:1: note: previous definition of ‘global’ was here

test.i

# 1 "test.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "test.c"
# 1 "test.h" 1
# 1 "global.h" 1
int global = 10;
# 2 "test.h" 2

const char* Name = "test.h";

char* hello_word()
{
    return "Hello world\n";
}
# 2 "test.c" 2
# 1 "global.h" 1
int global = 10;
# 3 "test.c" 2

int main()
{
    const char* s = hello_word();
    int g = global;

    return 0;
}



  • 条件编译可以解决头文件重复包含的编译错误
#ifndef _HEADER_FILE_H_
#define _HEADER_FILE_H_

// source code

#endif

test.h

#ifndef _TEST_H_
#define _TEST_H_

#include "global.h"

const char* Name = "test.h";

char* hello_word()
{
    return "Hello world\n";
}

#endif

global.h

#ifndef _GLOBAL_H_
#define _GLOBAL_H_

int global = 10;

#endif

test.i

# 1 "test.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "test.c"
# 1 "test.h" 1

# 1 "global.h" 1

int global = 10;
# 5 "test.h" 2

const char* Name = "test.h";

char* hello_word()
{
    return "Hello world\n";
}
# 2 "test.c" 2

int main()
{
    const char* s = hello_word();
    int g = global;

    return 0;
}

条件编译的意义

  • 条件编译使得我们可以按不同的条件编译不同的代码段,因而产生不同的代码段
  • #if...#else...#endif预编译器处理,而if...else...语句被编译器处理,必然被编译进目标代码
  • 实际工程中条件编译主要用于以下两种情况
        ○ 不同的产品线共用一份代码
        ○ 区分编译产品的调试版和发布版

实例分析: 产品线区分及调试代码应用

test.c

#include <stdio.h>
#include "product.h"

#if DEBUG
    #define LOG(s) printf("[%s:%d] %s\n", __FILE__, __LINE__, s)
#else
    #define LOG(s) NULL
#endif

#if HIGH
void f()
{
    printf("This is the high level product!\n");
}
#else
void f()
{
}
#endif

int main()
{
    LOG("Enter main() ...");
    
    f();
    
    printf("1. Query Information.\n");
    printf("2. Record Information.\n");
    printf("3. Delete Information.\n");
    
    #if HIGH
    printf("4. High Level Query.\n");
    printf("5. Mannul Service.\n");
    printf("6. Exit.\n");
    #else
    printf("4. Exit.\n");
    #endif
    
    LOG("Exit main() ...");
    
    return 0;
}

product.h

#ifndef _PRODUCT_H_
#define _PRODTCT_H_

#define DEBUG 1
#define HIGH  1

#endif
输出:
[test.c:20] Enter main()...
This is the high level product!.
1. Query Information.
2. Record Information.
3. Delete Information.
4. High Level Query.
5. Mannul Service.
6. Exit.
[test.c:33] Exit main() ...


小结

  • 通过编译器命令行能够定义预处理器使用的宏
  • 条件编译可以避免重复包含同一个头文件
  • 条件编译是在工程开发中可轻易区别不同产品线的代码
  • 条件编译可以定义产品的发布版和调试版

内容参考狄泰软件学院系列课程,如有侵权,请联系作者删除!感谢~

发布了334 篇原创文章 · 获赞 661 · 访问量 130万+

猜你喜欢

转载自blog.csdn.net/czg13548930186/article/details/86760614