您的位置:首页 > 编程语言 > C语言/C++

C语言条件编译指令

2012-11-16 10:30 253 查看
学了C语言和C++已经三个年头了,但是对于条件编译指令还不是很了解,看到Linux平台下地一些优秀的源代码,诸如uC/OS-II等用到了很多编译指令,MFC内部也用到了条件编译指令,现在把我学到的一些条件编译指令做下总结,以后学习到其他的编译指令,再添加进来

一、防止头文件重复包含

#ifndef _***_H

#define _***_H

程序段

#endif



这个条件编译指令经常用在头文件中,可以防止头文件重复包含。





二、#IFDEF | #IFNDEF ... #ENDIF Preprocessor Directive (from:http://technet.microsoft.com/zh-cn/library/t22e924w(v=vs.80))

Conditionally includes a set of commands at compile time if a compile-time constant is defined.

格式如下:

#IFDEF | #IFNDEF ConstantName 
      Commands
[#ELSE 
      Commands]
#ENDIF




Parameters

#IFDEF
Specifies that a set of commands is included at compile time when the
ConstantName is defined.

The following items describe how a set of commands is included at compile time when you include #IFDEF:

If ConstantName is defined, the set of commands following #IFDEF and preceding #ELSE or #ENDIF (whichever occurs first) is included at compile time.

If ConstantName is not defined and #ELSE is included, the set of commands following #ELSE and preceding #ENDIF is included at compile time.

If ConstantName is not defined and #ELSE is not included, no commands within the #IFDEF ... #ENDIF structure are included at compile time.

#IFNDEF
Specifies that a set of commands is included at compile time when the
ConstantName is not defined.

The following items describe how a set of commands is included at compile time when you include #IFNDEF:

If ConstantName is not defined, the set of commands following #IFNDEF and preceding #ELSE or #ENDIF (whichever occurs first) is included at compile time.

If ConstantName is defined and #ELSE is included, the set of commands following #ELSE and preceding #ENDIF is included at compile time.

If ConstantName is defined and #ELSE is not included, no commands within the #IFNDEF ... #ENDIF structure are included at compile time.

ConstantName
Specifies the compile-time constant whose existence determines whether a set of commands is included at compile time. Compile-time constants are defined with #DEFINE.

Commands
Specifies the set of commands that is included at compile time.

Remarks

You can nest an #IFDEF | #IFNDEF ... #ENDIF structure within another #IFDEF | #IFNDEF ... #ENDIF structure.

Comments can be placed on the same line after #IFDEF, #IFNDEF, #ELSE, and #ENDIF. These comments are ignored during compilation and program execution.



Example
The following example creates a compile-time constant named MYDEFINE. #IFDEF ... #ENDIF displays a message if the compile-time constant has been defined.

DEFINE MYDEFINE 1

#IFDEF MYDEFINE   
   WAIT WINDOW "MYDEFINE exists"
#ELSE
   WAIT WINDOW "MYDEFINE does not exist"
#ENDIF



有篇文章对这段英文文摘进行了中文翻译(来自http://hi.baidu.com/urzalknsyzchrur/item/e7deb293889b2649f1421582),如下:
#IFDEF | #IFNDEF ... #ENDIF 预处理器命令

编译期间,根据是否定义了某一个编译常量,决定一段代码是否要编译。

语法

#IFDEF | #IFNDEF ConstantName

Commands

[#ELSE

Commands]

#ENDIF

参数

#IFDEF

指定如果已定义了 ConstantName 指定的编译常量,则编译一段程序代码。

下面描述如何根据 #IFDEF 预处理器命令确定要编译的程序代码:

如果定义了 ConstantName,则要编译从 #IFDEF 到 #ELSE 或者 #ENDIF (取决于哪个在前面)之间的程序代

码。

如果没有定义 ConstantName,但有 #ELSE 命令,则编译 #ELSE 到 #ENDIF 之间的程序代码。

如果没有定义 ConstantName,也没有 #ELSE 命令,则 #IFDEF ... #ENDIF 结构中的所有程序都不编译。

#IFNDEF

当没有定义 ConstantName 指定的编译常量时,指定要编译的一组程序代码。

下面描述如何根据 #IFNDEF 预处理器命令决定要编译的程序代码:

如果没有定义 ConstantName,则编译 #IFNDEF 到 #ELSE 或者 #ENDIF(取决于哪个在前面)之间的程序代码



如果已经定义了 ConstantName,并且有 #ELSE 命令,则编译 #ELSE 到 #ENDIF

之间的程序代码。

如果定义了 ConstantName,但没有 #ELSE 命令,则 #IFNDEF ... #ENDIF 结构中的所有程序都不编译。

ConstantName

指定编译时间所用的常量。它的存在决定了是否编译某一组程序代码。编译常量由 #DEFINE 命令定义。

Commands

指定要编译的程序代码。

说明

一个 #IFDEF | #IFNDEF ... #ENDIF 结构可以嵌套另一个#IFDEF | #IFNDEF ... #ENDIF

结构。

注释可以放在 #IFDEF、#IFNDEF、#ELSE

和 #ENDIF 所在行的后面。这些注释在编译和程序运行期间将被忽略。

使用这条命令可以替代C和C++注释的功能,用例如下:
#ifdef CODE
Fuction       :  void sort_Insert(int iArray[], int size);
Content      :
Parameter  : iArray:int Array
                    size   :the length of array
Return        : void
Date           :   2012/11/16
Description :sort Array of int
Remark      :2012/11/16 Append
#endif




或者
#define CODE 1

#ifndef CODE
Function   : void sort_Insert(int iArray[],int size);
Content    : 
Parameter  : iArray  int数组
                       size    数组的元素个数
Return     :     none
Date       :      2012/11/16
Description: 对数组进行插入排序
Remark     : 2012/11/16 Append.
#endif



下面的文字转载自博客园newlist的一篇博文http://www.cnblogs.com/newlist/archive/2012/07/06/2579005.html

#ifdef

"#ifdef 语句1


  程序2


  #endif“


  可翻译为:如果宏定义了语句1则程序2。


  作用:我们可以用它区隔一些与特定头文件、程序库和其他文件版本有关的代码。


  代码举例:新建define.cpp文件


  #include "iostream.h"


  int main()


  {


  #ifdef DEBUG


  cout<< "Beginning execution of main()";


  #endif


  return 0;


  }


  运行结果为:


  Press any key to continue


  改写代码如下:


  #include "iostream.h"


  #define DEBUG


  int main()


  {


  #ifdef DEBUG


  cout<< "Beginning execution of main()";


  #endif


  return 0;


  }


  运行结果为:


  Beginning execution of main()


  Press any key to continue


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


  #define DEBUG


  #ifdef DEBUG


  #endif


  而在define.cpp源文件中,代码修改如下:


  #include "iostream.h"


  #include "head.h"


  [b]#define DEBUG[/b]


  int main()


  {


  #ifdef DEBUG


  cout<< "Beginning execution of main()";


  #endif


  return 0;


  }


  运行结果如下:


  Beginning execution of main()


  Press any key to continue


  结论:


  通过使用#ifdef指示符,我们可以区隔一些与特定头文件、程序库和其他文件版本有关的代码。


  C语言之详解#ifdef等宏


  这几个宏是为了进行条件编译。一般情况下,源程序中所有的行都参加编译。但是有时希望对其中一部分内容只在满足一定条件才进行编译,也就是对一部分内容指定编译的条件,这就是“条件编译”。有时,希望当满足某条件时对一组语句进行编译,而当条件不满足时则编译另一组语句。


  条件编译命令最常见的形式为:


  #ifdef 标识符


  程序段1


  #else


  程序段2


  #endif


  它的作用是:当标识符已经被定义过(一般是用#define命令定义),则对程序段1进行编译,否则编译程序段2。


  其中#else部分也可以没有,即:


  #ifdef


  程序段1


  #endif


  这里的“程序段”可以是语句组,也可以是命令行。这种条件编译可以提高C源程序的通用性。如果一个C源程序在不同计算机系统上系运行,而不同的计算机又有一定的差异。例如,我们有一个数据类型,在Windows平台中,应该使用long类型表示,而在其他平台应该使用float表示,这样往往需要对源程序作必要的修改,这就降低了程序的通用性。可以用以下的条件编译:


  #ifdef WINDOWS


  #define MYTYPE long


  #else


  #define MYTYPE float


  #endif


  如果在Windows上编译程序,则可以在程序的开始加上


  #define WINDOWS


  这样则编译下面的命令行:


  #define MYTYPE long


  如果在这组条件编译命令之前曾出现以下命令行:


  #define WINDOWS 0


  则预编译后程序中的MYTYPE都用float代替。这样,源程序可以不必作任何修改就可以用于不同类型的计算机系统。当然以上介绍的只是一种简单的情况,可以根据此思路设计出其它的条件编译。


  例如,在调试程序时,常常希望输出一些所需的信息,而在调试完成后不再输出这些信息。可以在源程序中插入以下的条件编译段:


  #ifdef DEBUG


  print ("device_open(%p)\n", file);


  #endif


  如果在它的前面有以下命令行:


  #define DEBUG


  则在程序运行时输出file指针的值,以便调试分析。调试完成后只需将这个define命令行删除即可。有人可能觉得不用条件编译也可达此目的,即在调试时加一批printf语句,调试后一一将printf语句删除去。的确,这是可以的。但是,当调试时加的printf语句比较多时,修改的工作量是很大的。用条件编译,则不必一一删改printf语句,只需删除前面的一条“#define DEBUG”命令即可,这时所有的用DEBUG作标识符的条件编译段都使其中的printf语句不起作用,即起统一控制的作用,如同一个“开关”一样。


  有时也采用下面的形式:


  #ifndef 标识符


  程序段1


  #else


  程序段2


  #endif


  只是第一行与第一种形式不同:将“ifdef”改为“ifndef”。它的作用是:若标识符未被定义则编译程序段1,否则编译程序段2。这种形式与第一种形式的作用相反。


  以上两种形式用法差不多,根据需要任选一种,视方便而定。


  还有一种形式,就是#if后面的是一个表达式,而不是一个简单的标识符:


  #if 表达式


  程序段1


  #else


  程序段2


  #endif


  它的作用是:当指定的表达式值为真(非零)时就编译程序段1,否则编译程序段2。可以事先给定一定条件,使程序在不同的条件下执行不同的功能。


  例如:输入一行字母字符,根据需要设置条件编译,使之能将字母全改为大写输出,或全改为小写字母输出。


  #define LETTER 1


  main()


  {


  char str[20]="C Language",c;


  int i=0;


  while((c=str[i])!='\0'){


  i++;


  #if LETTER


  if(c>='a'&&c<='z') c=c-32;


  #else


  if(c>='A'&&c<='Z') c=c+32;


  #endif


  printf("%c",c);


  }


  }


  运行结果为:C LANGUAGE


  现在先定义LETTER为1,这样在预处理条件编译命令时,由于LETTER为真(非零),则对第一个if语句进行编译,运行时使小写字母变大写。如果将程序第一行改为:


  #define LETTER 0


  则在预处理时,对第二个if语句进行编译处理,使大写字母变成小写字母(大写字母与相应的小写字母的ASCII代码差32)。此时运行情况为:


  c language


  有人会问:不用条件编译命令而直接用if语句也能达到要求,用条件编译命令有什么好处呢?的确,此问题完全可以不用条件编译处理,但那样做目标程序长(因为所有语句都编译),而采用条件编译,可以减少被编译的语句,从而减少目标的长度。当条件编译段比较多时,目标程序长度可以大大减少。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: