【问题标题】:#ifdef inside #define#ifdef 内 #define
【发布时间】:2011-07-31 23:33:16
【问题描述】:

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

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

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

【问题讨论】:

标签: c c-preprocessor


【解决方案1】:

只需翻转它:

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

【讨论】:

  • 感谢您的回复。我尝试了 COV_ON(on) 和 COV_ON(off),但随后我收到一条错误消息:error:expected ')',near off。关于这个问题的任何想法。
  • 它们是字符串。我需要将它们用作引号内的常规字符串吗?比如“开”和“关”。
  • 这取决于您对 COVERAGE 的定义,但我猜您需要引用它们。我建议你为这个新问题打开另一个问题。
  • 我用引号试过了,但没用。我已经打开了一个新线程。如果您认为 _Pragma 的语法有误,请告诉我。
【解决方案2】:
#ifdef COVERAGE_TOOL
    #define COV_ON(x) _Pragma (COVERAGE #x)
#else
    #define COV_ON(x)
#endif

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 感谢您的回复。我尝试了 COV_ON(on) 和 COV_ON(off) 但随后我收到一条错误消息:error:expected ')',near off。关于这个问题的任何想法。
    • 在不了解您的编译器的情况下,我们需要知道预期的 _Pragma 语法是什么;如果没有宏,你会怎么做?
    • 我正在使用 VC2005 编译器,并在网上看到了 _Pragma 语法。我以前从未以这种方式使用过#pragma。
    • 那么你需要一个 stringify hack。见this question and answer
    • 我猜是VC2005的问题。他们不使用 _Pragma,而是使用 __pragma。我不确定,但看起来是这样的。但是我仍然收到带有 __pragma 的警告 unknown pragma。
    【解决方案4】:

    不可能。反过来做:

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

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        【解决方案6】:

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

        您可以选择性地定义一个 __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) )
        

        这与 ifdef 具有相似的功能,除了你用括号来描述开始和结束(大多数 IDE 没有代码折叠问题)虽然你仍然可以在上下文中使用 #define#ifdef,但 @987654325 @ 不允许。为了获得类似于#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 中只有一个会生成代码。

        好的,这是一个方便的 hack,但是我们可以让它MORE#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*/
        )
        

        【讨论】:

        • 我喜欢你的风格(=
        猜你喜欢
        • 2010-12-20
        • 1970-01-01
        • 2011-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-26
        • 1970-01-01
        • 2015-08-08
        相关资源
        最近更新 更多