c语言 条件编译的使用分析,【C】 22_条件编译使用分析

基本概念

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

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

示例分析: 条件编译初探

test.c

#include

#define C 1

int main()

{

const char* s;

#if( C == 1 )

s = "This is first printf...\n";

#else

s = "This is second printf...\n";

#endif

printf("%s", 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

#include的本质

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

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

bVbkuVf?w=319&h=250

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

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

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;

输出:

global.h:2: error: redefinition of ‘global’

global.h:2: note: previous definition of ‘global’ was here

test.i

# 1 "test.c"

# 1 ""

# 1 ""

# 1 "test.c"

# 1 "test.h" 1

# 1 "global.h" 1

int global = 10;

# 3 "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 ""

# 1 ""

# 1 "test.c"

# 1 "test.h" 1

# 1 "global.h" 1

int global = 10;

# 3 "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

#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

#define DEBUG 1

#define HIGH 1

输出:

[test.c:23] 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:39] Exit main() ...

小结

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

条件编译可以避免重复包含同一个头文件

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

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

以上内容参考狄泰软件学院系列课程,请大家保护原创!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值