【问题标题】:print only if _DEBUG defined: c仅在 _DEBUG 定义时打印:c
【发布时间】:2023-03-15 04:23:01
【问题描述】:

我只想在定义_DEBUG 时打印信息

#define DEBUG(y) y == true ? #define _DEBUG true  : #define _DEBUG false

#ifdef _DEBUG
#define Print(s)  printf(s); 
#endif

出现错误:

error: '#' is not followed by a macro parameter

任何建议如何使用预处理器指令来实现这一点?

我打算将它从我的主要用作:

DEBUG(true);
Print("Inside main in debug mode");

【问题讨论】:

  • 据我了解,预处理器语句是在编译之前进行评估的。因此,预处理将在应用您的任何逻辑之前评估 #Define 语句。 @JohnZwinck 采用与我相同的方法。

标签: c preprocessor-directive


【解决方案1】:

cannotrun-time 重新定义宏。 你也不能有#define inside of another #define,就像你在代码的第一行尝试一样。

你可以这样做:

#ifdef _DEBUG
#define Print(s)  printf("%s", s)
#else
#define Print(s)  
#endif

并从你的主要使用它:

#define _DEBUG
Print("Inside main in debug mode");
#undef _DEBUG
Print("Inside main debug mode off");

如果你真的需要在运行时打开和关闭调试,你可以这样做:

void PrintIf(BOOL dbg, char * msg)
{
   if (dbg)
   {
       printf("%s", msg)
   }
}

并像这样使用它

y = TRUE;
PrintIf(y,"Inside main in debug mode");
y = FALSE;
PrintIf(y,"Inside main debug mode off");

【讨论】:

    【解决方案2】:

    试试这个:

    #ifdef DEBUG
    #define Print(s) printf("%s", s)
    #else
    #define Print(s)
    #endif
    

    然后:

    #define DEBUG
    Print("Inside main in debug mode");
    

    【讨论】:

    • 除非我在这里遗漏了什么,否则这是行不通的。 Print 宏将在解析第一个代码块 (#ifdef DEBUG) 时定义,因此在使用 Print 宏之前调用 #define DEBUG#undef DEBUG 将无效。也就是说,Print 所做的事情将在它到达#define DEBUG 行时已经确定。
    • @Cookyt:你说得对,#define DEBUG 需要出现在任何定义 Print() 的 #include 之前。或者,可以在完全没有预处理器的情况下将检查变成“运行时”(但可能进行优化)。
    【解决方案3】:

    我打算将它从我的主要用作:

    DEBUG(y);
    Print("Inside main in debug mode");
    

    抱歉,ifdef 是编译时(不是运行时)。您可以使用全局布尔值和运行时检查来启用和禁用调试。

    【讨论】:

      【解决方案4】:

      您不能像尝试那样使用宏创建预处理器语句;它不起作用,也是不允许的。条件打印见C #define macro for debug printing

      【讨论】:

        【解决方案5】:

        问题出现在这里:

        #define DEBUG(y) y == true ? #define _DEBUG true  : #define _DEBUG false
        

        在引入#defines 时,# 的定义出现在行首,而不是稍后(尽管)预处理器通常允许一到两个行缩进。)您需要重写您的#define,简单地消除三元运算符如:

        #ifdef _DEBUG
        #define Print(s)  printf(s); 
        #endif
        

        虽然您可以使用宏扩展您的定义,但您经常会引入额外的错误。通常最好坚持将_DEBUG 代码简单地包装在#ifdef 语句中:

        #ifdef _DEBUG
            fprintf (stderr, "your error messages\n");  // using standard printf/fprintf instead of macros
            ...
        #endif  /* _DEBUG */
        

        【讨论】:

          【解决方案6】:

          宏在预处理阶段被替换并且

          #define DEBUG(y) y == true ? #define _DEBUG true  : #define _DEBUG false
          

          此语句将在编译时进行评估。

          条件运算符(三元运算符)在编译时计算。所以你得到了这个错误,并且必须始终在语句的开头使用 # 运算符,这是你正在做的第二个错误。

          这样你可以更好地使用它

          #define DEBUG
          printf ("true");
          #else
          printf ("false");
          

          您还可以使用 gcc 选项 -D 动态定义此宏

          gcc -D DEBUG filename.c -o outputFile
          

          【讨论】:

            【解决方案7】:

            第一行不正确:

            #define DEBUG(y) y == true ? #define _DEBUG true  : #define _DEBUG false
            

            您不能在预处理器指令中使用#define(如另一个#define

            这是没有意义的,因为preprocessing 发生在真正的编译之前(所以在运行时间之前,当你的y 有一些价值时)。阅读cpp preprocessor documentation。回想一下,有时预处理器甚至是不同的程序 (/lib/cpp),但现在是大多数 C 编译器的第一阶段。

            您可以要求源代码的预处理形式(例如,如果使用 GCC,则使用 gcc -C -E source.c > source.i)并使用寻呼机 (less source.i) 或编辑器查看该形式。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-04-28
              • 2021-11-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-12-06
              • 1970-01-01
              相关资源
              最近更新 更多