问题描述
我想有一个调试模式,所以如果
I'm trying to have a debugging mode on so if
#define DEBUG 1
我想给printf一些变量的值,如果
I want to printf some variable values and if
#define DEBUG 0
我想他们了。
问题是我有很多实现文件,我想这DEBUG变量可用于整个项目。现在我需要在foo1.c,foo2.c,foo3.c这似乎繁琐和容易出错且必须有一个更好的方式来编辑DEBUG变量。有什么建议?
The problem is I have many implementation files and I want this DEBUG variable to be available for the whole project. Right now I need to edit the DEBUG variable in foo1.c, foo2.c, foo3.c which seems tedious and error-prone and there must be a better way. Any suggestions?
推荐答案
编译时,你应该能够指定一个选项,你的编译器。例如,您可以调用GCC与 -DDEBUG
选项。
When compiling, you should be able to specify an option to your compiler. For example, you can call GCC with the -DDEBUG
option.
在这种情况下,你会过得更好用:
In this case, you would be better using:
#ifdef DEBUG
#endif
或
#if defined(DEBUG)
#endif
如果这不是你现在做的方式。我很惊讶,你没有为你的项目全局头文件中。沿着线的东西:
if this is not the way you're doing it now. I'm surprised that you don't have a global header file for your project. Something along the lines of:
#ifdef DEBUG
#undef DEBUG
#endif
#define DEBUG 1
在一个名为debug.h的文件。在C程序中,可以通过使用包括本的#includedebug.h
in a file called "debug.h". In your C programs, you can include this by using #include "debug.h"
这篇关于将#define DEBUG 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!