【问题标题】:DDEBUG=0 doesn't workDDEBUG=0 不起作用
【发布时间】:2016-12-25 09:34:40
【问题描述】:

我正在学习 gcc 选项 DDEBUG。 下面是我的简单测试代码:

#include <stdio.h>
#include <stdlib.h>


#ifdef DEBUG
    #define debug(msg) printf("Debug: %s\n", msg)
#else
    #define debug(msg)
#endif

int main(int argc, char const *argv[])
{

    debug("Debug flag was defined\n");

    printf("hello world\n");
    return 0;
}

然后,我用gcc -DDEBUG=0 debug.c 编译,我希望“Debug flag was defined”不会被打印出来,但确实如此。我可以知道为什么-DDEBUG=0 不起作用吗?

【问题讨论】:

  • 也许你把 #ifdef#if 混淆了

标签: c debugging gcc c-preprocessor


【解决方案1】:

预处理器条件不是这样工作的。当您将DEBUG 定义为等于某个值时(不管是哪个值),它仍然是已定义,这意味着#ifdef 将是“真”。

要么根本不定义宏(这是“正常”的方式),要么使用#iflike

#if DEBUG != 0
    #define debug(msg) printf("Debug: %s\n", msg)
#else
    #define debug(msg)
#endif

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 2016-01-07
    相关资源
    最近更新 更多