我想写的东西是这样的:的#ifdef
#define COV_ON(x) \
#ifdef COVERAGE_TOOL \
_Pragma (COVERAGE #x)
#endif
有什么办法来定义COV_ON
这样吗?我知道我在上面做的是错误的,因为我在#define中不能有#ifdef
。 (#
不是#define
中的允许字符)。 那么有什么解决办法吗?
我想写的东西是这样的:的#ifdef
#define COV_ON(x) \
#ifdef COVERAGE_TOOL \
_Pragma (COVERAGE #x)
#endif
有什么办法来定义COV_ON
这样吗?我知道我在上面做的是错误的,因为我在#define中不能有#ifdef
。 (#
不是#define
中的允许字符)。 那么有什么解决办法吗?
不可能的。反过来:
#ifdef COVERAGE_TOOL
#define COV_ON(x) _Pragma (COVERAGE #x)
#else
#define COV_ON(x)
#endif
简单地将其翻过来:
#ifdef COVERAGE_TOOL
#define COV_ON(x) _Pragma (COVERAGE #x)
#else
#define COV_ON(x) /* foo */
#endif
#ifdef COVERAGE_TOOL
#define COV_ON(x) _Pragma (COVERAGE #x)
#else
#define COV_ON(x)
#endif
你不能。但是你可以换#ifdef
和#define
:
#ifdef COVERAGE_TOOL
# define COV_ON(x) _Pragma (COVERAGE #x)
#else
# define COV_ON(x)
#endif
感谢您的回复。我尝试了COV_ON(on)和COV_ON(off),但后来出现错误,说错误:预计')',接近关闭。对这个问题有任何想法。 – 2011-04-07 20:40:16
不知道你的编译器,我们需要知道预期的'_Pragma'语法是什么;你如何在没有宏观的情况下做到这一点? – 2011-04-07 20:55:53
我使用VC2005编译器,并在线看到_Pragma语法。我从来没有以这种方式使用过#pragma。 – 2011-04-07 21:06:23
正如你所说,在#define中不可能有#ifdef。你应该做的是相反的顺序:
#ifdef COVERAGE_TOOL \
#define COV_ON(x) \
etc.
#endif
这是一个古老的问题,但它需要一个最新的答案。
而不是使用宏内内嵌的ifdef,你可以选择性地定义一个__VA_ARGS__
宏做同样的事情
#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))
此功能类似于一个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
)
只有NO_FOO()/IF_FOO
一个会产生代码。
好的,这是一个方便的黑客攻击,但我们可以让它更多有用比#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
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*/
)
感谢您的答复。我尝试了COV_ON(on)和COV_ON(off),但后来出现错误,说错误:预计')',接近关闭。对这个问题有任何想法。 – 2011-04-07 20:31:52
什么是“开”和“关”? – Philip 2011-04-07 20:38:00
他们是字符串。我需要在报价单内使用它们作为常规字符串吗?像“开”和“关”。 – 2011-04-07 20:47:26