简体   繁体   English

#ifndef指令的用法

[英]Usage of #ifndef directive

I'm trying to use #ifndef as below. 我正在尝试如下使用#ifndef。

#ifndef MACRO1 || #ifndef MACRO2
....
#endif

I already tried: 我已经尝试过:

#ifndef (MACRO1 || MACRO2) 
..
#endif

But for both cases am getting below error 但是对于这两种情况,都低于错误

error: extra tokens at end of #ifndef directive 错误:#ifndef指令末尾的额外令牌

请使用#if预处理程序指令:

#if !defined(MACRO1) || !defined(MACRO2)

You can use following code 您可以使用以下代码

#if !defined(MACRO1) || !defined(MACRO2)
#endif

You can use the defined operator in the #if directive to use expressions that evaluate to 0 or 1 within a preprocessor line. 您可以在#if指令中使用已定义的运算符,以使用在预处理程序行中计算为0或1的表达式。

#ifdef and #ifndef are special abbreviations for #if defined(...) and #if !defined(...) . #ifdef#ifndef#if defined(...)#if !defined(...)特殊缩写。 However, they can only be used for a single macro and do not allow for logical operations. 但是,它们只能用于单个宏,并且不允许进行逻辑运算。 So, if checking for multiple macros, use #if with the defined() operator instead. 因此,如果要检查多个宏,请改用#if#if defined()运算符。 Being a regular operatior, this can be combined with logical operations, as the for !defined() already does. 作为常规操作员,可以将它与逻辑操作结合使用,就像for !defined()一样。

You can use logical operators in preprocessor directives, but in order to check something defined, use the defined directive: 您可以在预处理程序指令中使用逻辑运算符,但是要检查已定义的内容,请使用已defined指令:

#if !defined MACRO1 || !defined MACRO2
....
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM