0


defined 用法

这就是#if defined 的唯一作用!

1) #if defined XXX_XXX

#endif 是条件编译,是根据你是否定义了XXX_XXX这个宏,而使用不同的代码。

一般.h文件里最外层的

#if !defined XXX_XXX

#define XXX_XXX

#endif 是为了避免.h头文件被重复include。

2) #error XXXX 是用来产生编译时错误信息XXXX的,一般用在预处理过程中;

例子:

#if !defined(__Dataauto)

#error C++ compiler required.

#endif

引用别人的英文用法说明:

The special operator defined is used in #if and #elif expressions to test whether a certain name is defined as a macro.

defined name and defined (name) are both expressions whose value is 1 if name is defined as a macro at the current point in the program, and 0 otherwise.

Thus, #if defined MACRO is precisely equivalent to #ifdef MACRO. defined is useful when you wish to test more than one macro for existence at once.

For example, #if defined (vax) || defined (ns16000) would succeed if either of the names vax or ns16000 is defined as a macro. Conditionals written like this: #if defined BUFSIZE && BUFSIZE >= 1024 can generally be simplified to just #if BUFSIZE >= 1024, since if BUFSIZE is not defined, it will be interpreted as having the value zero.

If the defined operator appears as a result of a macro expansion, the C standard says the behavior is undefined. GNU cpp treats it as a genuine defined operator and evaluates it normally. It will warn wherever your code uses this feature if you use the command-line option -pedantic, since other compilers may handle it differently .

好文要顶 关注我 收藏该文

标签:

本文转载自: https://blog.csdn.net/juruiyuan111/article/details/115466288
版权归原作者 juruiyuan111 所有, 如有侵权,请联系我们删除。

“defined 用法”的评论:

还没有评论