C预处理器条件指令优先级和嵌套

时间:2014-11-21 04:14:41

标签: c c-preprocessor

我有一些代码具有以下形式的现有预处理器条件指令:

#ifndef SYMBOL_XYZ
// some code here
#else
// some other code here
#endif

我希望添加一个取代该逻辑的新条件,我认为这是实现它的方法,但我不确定嵌套和优先级在C预处理器方面的细微之处。

#ifdef NEW_SYMBOL_ABC
// some new code here that takes precedence over the other two conditions
#else
   #ifndef SYMBOL_XYZ
   // some code here
   #else
   // some other code here
   #endif
#endif

我有这个权利吗?它会等同于:

#ifndef NEW_SYMBOL_ABC
   #ifndef SYMBOL_XYZ
   // some code here
   #else
   // some other code here
   #endif
#else
   // some new code here that takes precedence over the other two conditions
#endif

1 个答案:

答案 0 :(得分:2)

试试这个......

#ifdef NEW_SYMBOL_ABC
// some new code here that takes precedence over the other two conditions
#elif  !defined(SYMBOL_XYZ)
// some code here
#else
// some other code here 
#endif

以上是我常用的,应该肯定与gcc一起使用。 不确定,但应该使用visual c ++和其他编译器。