C 调试宏异常行为
我有这样一个文件:C 调试宏异常行为,c,debugging,compilation,C,Debugging,Compilation,我有这样一个文件: #include <stdio.h> #include <string.h> #include <stdlib.h> #define DEBUG int main(void) { #ifdef DEBUG printf("We are in debug mode"); #endif } #包括 #包括 #包括 #定义调试 内部主(空){ #ifdef调试 printf(“我们处于调试模式”); #恩迪夫 } 我被告知使用i
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define DEBUG
int main(void) {
#ifdef DEBUG
printf("We are in debug mode");
#endif
}
#包括
#包括
#包括
#定义调试
内部主(空){
#ifdef调试
printf(“我们处于调试模式”);
#恩迪夫
}
我被告知使用ifdef和endif(用于此)。
我的问题发生在我使用makefile(不允许编辑)编译此文件时。发生的是print语句(Debug one)打印,这不应该,因为我没有处于调试模式。我尝试使用这个命令(在Ubuntu 14.04上)
make-DEBUG
但这做了一些完全不同的事情,输出文件打印ifdef语句,尽管没有处于调试模式。
我做错了什么?您在源文件中显式定义了调试
#define DEBUG
删除该行,以便不重写生成环境中的任何定义。您在源文件中显式定义调试
#define DEBUG
删除该行,以便不重写生成环境中的任何定义。您在源文件中显式定义了
DEBUG
(使用#define DEBUG
),因此无论如何构建,您都将始终处于调试模式
删除#define DEBUG
它将处于非调试模式,除非在编译命令行上设置了调试
使用命令
make CPPFLAGS=-DDEBUG
使用makefile进行构建,并将-DDEBUG
参数添加到预处理器标志中,以便在调试模式下构建(注意两个D
s)您在源文件中显式定义debug
(使用\define debug
),因此,无论如何构建,您都将始终处于调试模式
删除#define DEBUG
它将处于非调试模式,除非在编译命令行上设置了调试
使用命令make CPPFLAGS=-DDEBUG
使用makefile进行构建,并将-DDEBUG
参数添加到预处理器标志中,以便在调试模式下进行构建(注意两个D
s)
-D选项用于运行时宏
还请注意
如果DEF仅检查定义,则不会检查值是否为true或false
------------------------
#define DEBUG 0
if (DEBUG)
will not enter here
#ifdef DEBUG
will enter here
$endif
------------------------
#define debug 0
int main()
{
if (debug) {
printf("Hello, World!2\n");
}
#ifdef debug
printf("Hello, World!3\n");
#endif
return 0;
}
-----------------------------------
output:
Hello, World!3
-D选项用于运行时宏
还请注意
如果DEF仅检查定义,则不会检查值是否为true或false
------------------------
#define DEBUG 0
if (DEBUG)
will not enter here
#ifdef DEBUG
will enter here
$endif
------------------------
#define debug 0
int main()
{
if (debug) {
printf("Hello, World!2\n");
}
#ifdef debug
printf("Hello, World!3\n");
#endif
return 0;
}
-----------------------------------
output:
Hello, World!3
从C文件中删除#define。然后使用makefile,因为makefile将启用/禁用预处理器“DEBUG”。然后使用makefile,因为makefile将启用/禁用预处理器“调试”。我的教授让我这么做?不确定我是否误解了它,或者其他什么。我的教授让我这么做的?不确定我是否误解了它,或者其他什么。我被告知要使用“make-DEBUG”,那么我还必须删除“define DEBUG”吗?我被告知要使用“make-DEBUG”,那么我还必须删除“define DEBUG”吗?虽然这段代码可能会回答这个问题,提供有关此代码回答问题的原因和/或方式的附加上下文可以提高其长期价值。虽然此代码可以回答问题,但提供有关此代码回答问题的原因和/或方式的附加上下文可以提高其长期价值。