C语言 #ifdef 里面 #define

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5586429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:18:48  来源:igfitidea点击:

#ifdef inside #define

cc-preprocessor

提问by agent.smith

I am trying to write something like this:

我正在尝试写这样的东西:

#define COV_ON(x) \
                #ifdef COVERAGE_TOOL \
                    _Pragma (COVERAGE #x)
                #endif

Is there any way to define COV_ONlike this? I know what I have done above is wrong as I can't have #ifdefinside #define. (#is not an allowed character in #define). So is there any solution?

有没有办法COV_ON像这样定义?我知道我在上面所做的事情是错误的,因为我不能#ifdef在#define 中进行操作。(#中不允许使用的字符#define)。那么有什么解决办法吗?

回答by Hans Passant

Not possible. Do it the other way around:

不可能。反过来做:

#ifdef COVERAGE_TOOL
#define COV_ON(x) _Pragma (COVERAGE #x)
#else
#define COV_ON(x)
#endif

回答by Philip

Simply turn it around:

简单地扭转它:

#ifdef COVERAGE_TOOL
#define COV_ON(x) _Pragma (COVERAGE #x)
#else
#define COV_ON(x) /* foo */
#endif

回答by technosaurus

This is an old question, but it needed an up to date answer.

这是一个古老的问题,但它需要一个最新的答案。

Instead of using an inline ifdef within the macro you can selectively define a __VA_ARGS__macro to do the same thing

您可以有选择地定义一个__VA_ARGS__宏来做同样的事情,而不是在宏中使用内联 ifdef

#ifdef COVERAGE_TOOL
#define IF_COVERAGE_TOOL(...) __VA_ARGS__
#else
#define IF_COVERAGE_TOOL(...)
#endif
#define COV_ON(x) IF_COVERAGE_TOOL( _Pragma (COVERAGE #x) )

This has similar functionality to an ifdef except that you get parentheses to delineate the beginning and end (which most IDEs have no problems code-folding) While you can still use #defineand #ifdefwithin the context, #includeis not allowed. In order to get inline capabilities similar to #else, you can define a corresponding macro like this:

这与 ifdef 具有类似的功能,除了您使用括号来描述开头和结尾(大多数 IDE 代码折叠没有问题)虽然您仍然可以在上下文中使用#define和,但这是不允许的。为了获得类似于 的内联功能,您可以像这样定义相应的宏:#ifdef#include#else

//#define FOO
#ifdef FOO
#define IF_FOO(...) __VA_ARGS__ 
#define NO_FOO(...)
#else
#define IF_FOO(...)
#define NO_FOO(...) __VA_ARGS__
#endif

IF_FOO(
  #define BAR 5
  int foo = BAR;
)
NO_FOO(
  #define foo 5
)

Only one of NO_FOO()/IF_FOOwill produce code.

只有其中之一NO_FOO()/IF_FOO会产生代码。

OK, that's a handy hack, but can we make it MOREuseful than #ifdefs... Boolean logic and configuration perhaps? Lets set up some truth tables (and a couple helper macros).

OK,这是一个方便的黑客,但我们可以让更多有用比#ifdefs...布尔逻辑和配置吧?让我们设置一些真值表(和几个辅助宏)。

#define PASTE_(x,y) x##y
#define PASTE(x,y) PASTE_(x,y)
#define PASTE3_(x,y,z) x##y##z
#define PASTE3(x,y,z) PASTE3_(x,y,z)
#define Y(...) __VA_ARGS__
#define N(...)
#define IF(x) x //alternate method similar to IFNOT()

#define NOT_N Y
#define NOT_Y N
#define IF_NOT(x) PASTE(NOT_,x)
#define NOT(x) PASTE(NOT_,x)

#define N_OR_N N
#define N_OR_Y Y
#define Y_OR_N Y
#define Y_OR_Y Y
#define OR(x,y) PASTE3(x,_OR_,y)

#define N_AND_N N
#define N_AND_Y N
#define Y_AND_N N
#define Y_AND_Y Y
#define AND(x,y) PASTE3(x,_AND_,y)

#define N_XOR_N N
#define N_XOR_Y Y
#define Y_XOR_N Y
#define Y_XOR_Y N
#define XOR(x,y) PASTE3(x,_XOR_,y)

#define N_NOR_N Y
#define N_NOR_Y N
#define Y_NOR_N N
#define Y_NOR_Y N
#define NOR(x,y) PASTE3(x,_NOR_,y)

#define N_NAND_N Y
#define N_NAND_Y Y
#define Y_NAND_N Y
#define Y_NAND_Y N
#define NAND(x,y) PASTE3(x,_NAND_,y)

#define N_XNOR_N Y
#define N_XNOR_Y N
#define Y_XNOR_N N
#define Y_XNOR_Y Y
#define XNOR(x,y) PASTE3(x,_XNOR_,y)

#define IF2(x,y,z) PASTE3(x,y,z)

config.h

配置文件

#define FOO Y
#define BAR N
#define BAZ Y

code.c

代码.c

AND(FOO,BAR)(/*do stuff if both FOO and BAR are enabled*/)
IF2(FOO,_AND_,BAR)( /*do stuff if both FOO and BAR are enabled*/ )
OR(BAZ,AND(FOO,BAR))(
  /*do stuff if both FOO and BAR are enabled or BAZ is enabled*/
)

回答by EboMike

#ifdef COVERAGE_TOOL
    #define COV_ON(x) _Pragma (COVERAGE #x)
#else
    #define COV_ON(x)
#endif

回答by sam hocevar

You cannot. But you can swap #ifdefand #define:

你不能。但是你可以交换#ifdef#define

#ifdef COVERAGE_TOOL
#   define COV_ON(x) _Pragma (COVERAGE #x)
#else
#   define COV_ON(x)
#endif

回答by jberg

As you mentioned it is not possible to have an #ifdef in a #define. What you should do instead is reverse the order:

正如您所提到的,#define 中不可能有 #ifdef。你应该做的是颠倒顺序:

#ifdef COVERAGE_TOOL \
  #define COV_ON(x) \
    etc.
#endif