简体   繁体   English

#定义调试1

[英]#define DEBUG 1

I'm trying to have a debugging mode on so if我正在尝试打开调试模式,如果

#define DEBUG 1

I want to printf some variable values and if我想打印一些变量值,如果

#define DEBUG 0

I want them off.我要他们离开。

The problem is I have many implementation files and I want this DEBUG variable to be available for the whole project.问题是我有很多实现文件,我希望这个 DEBUG 变量可用于整个项目。 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.现在我需要编辑 foo1.c、foo2.c、foo3.c 中的 DEBUG 变量,这看起来很乏味且容易出错,必须有更好的方法。 Any suggestions?有什么建议?

When compiling, you should be able to specify an option to your compiler.编译时,您应该能够为编译器指定一个选项。 For example, you can call GCC with the -DDEBUG option.例如,您可以使用-DDEBUG选项调用 GCC。

In this case, you would be better using:在这种情况下,您最好使用:

#ifdef DEBUG
#endif

or:或者:

#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:类似的东西:

#undef DEBUG
#define DEBUG 1

in a file called "debug.h".在名为“debug.h”的文件中。 In your C programs, you can include this by using #include "debug.h"在你的 C 程序中,你可以使用#include "debug.h"来包含它

Try something like Steve McConnel suggests in section 6 of "Chapter 8: Defensive Programming" from Code Complete 2 ... Add this to your code:尝试类似 Steve McConnel 在Code Complete 2的“第 8 章:防御性编程”的第 6 节中建议的内容……将其添加到您的代码中:

#ifdef DEBUG
#if (DEBUG > 0) && (DEBUG < 2)
printf("Debugging level 1");
#endif

#if (DEBUG > 1) && (DEBUG < 3)
printf("Debugging level 2");
#endif

#if (DEBUG > n-1) && (DEBUG < n)
printf("Debugging level n");
#endif
#endif

Then when you compile, add this flag (warning: This might be compiler-dependent):然后在编译时添加此标志(警告:这可能与编译器有关):

-DDEBUG=m

Or, have a global header that defines these sorts of things, as others have suggested.或者,正如其他人所建议的那样,拥有一个定义此类事物的全局标头。

As a response to your problem you can also simply invoke the compiler like:作为对您的问题的回应,您还可以简单地调用编译器,如:

cc -c -DDEBUG=1 

or或者

 cc -c -DDEBUG=0

You must delete the "define DEBUG 1/0" in your files - or replace it with:您必须删除文件中的“定义调试 1/0” - 或将其替换为:

#ifndef DEBUG
#define DEBUG 0
#endif

Here is what I am using (GCC syntax):这是我正在使用的(GCC 语法):

  • create a file debug.h with the following content and include it in each c file:使用以下内容创建文件 debug.h 并将其包含在每个 c 文件中:

     #ifdef DEBUG extern FILE *dbgf; #define D_MIN 0x00010000 // Minimum level #define D_MED 0x00020000 // Medium level #define D_MAX 0x00040000 // Maximum level #define D_FLUSH 0x00080000 // Usefull by a program crash #define D_TRACE 0x00100000 #define D_1 0x00000001 ... #define D(msk, fmt, args...) if(msk & dbgmsk) { fprintf(dbgf, "%s:",__FUNCTION__); fprintf(dbgf, fmt, ## args ); if(msk & D_FLUSH) fflush(dbgf); } #define P(msk, fmt, args...) if(msk & dbgmsk) { fprintf(dbgf, fmt, ## args ); if(msk & D_FLUSH) fflush(dbgf); } #else #define D(msk, fmt, args...) #define P(msk, fmt, args...) #endif

dbgmsk is variable, which can be global (whole program) or local/static and must be initialized a start. dbgmsk 是变量,它可以是全局的(整个程序)或局部的/静态的,并且必须在开始时进行初始化。 You can define several options for the whole program or for each module.您可以为整个程序或每个模块定义多个选项。 This is better and more flexible than the version with the level variable.这比带有 level 变量的版本更好、更灵活。

Ex.前任。 module1.c:模块1.c:

#include "debug.h"

static int dbgmsk;  // using local dbgmsk
module1_setdbg(int msk) { dbgmsk = msk; D(D_TRACE,"dbgmsk1=%x\n", dbgmsk); }

foo1() {  P(D_1, "foo1 function\n" ); 
  ....
}
foo2() {}
...

foo3.c foo3.c

#include "debug.h"
extern int dbgmsk; // using global dbgmsk

Ex.前任。 main:主要的:

#include "debug.h"
FILE *dbgf;
int dbgmsk = 0; // this is the global dbgmsk

int main() { 
  dbgf = stderr; // or your logfile
  dbgmsk = D_MIN;
  module1_setdbg(D_MIN|D_MED|D_TRACE|D_1);
  ....
}

I'm also storing all dbgmsk variables in a config text file that is read at the program start.我还将所有 dbgmsk 变量存储在程序启动时读取的配置文本文件中。

将“#define DEBUG”放在“debug.h”中,并在每个*.c 文件中#include 该头文件。

As @person-b says, specify this define as a compiler option, eg -D DEBUG正如@person-b 所说,将此定义指定为编译器选项,例如 -D DEBUG

Note though that to simplify this you should change the test in your code from:请注意,为了简化这一点,您应该将代码中的测试更改为:

#if DEBUG

to:到:

#ifdef DEBUG

This way you don't have to worry about specifying a 0 or 1 value but can instead rely on it being defined or not.这样您就不必担心指定 0 或 1 值,而是可以依赖于它是否被定义。

The suggestion from samoz and Stephen Doyle to check for the existence of a definition for DEBUG rather than its value is a good one. samoz 和 Stephen Doyle 建议检查 DEBUG 的定义是否存在而不是其值,这是一个很好的建议。 However, if you really want to use DEBUG=0, this is how you can do it: Each time you define the DEBUG flag (ie, in each file), check for an existing definition:但是,如果您真的想使用 DEBUG=0,您可以这样做:每次定义 DEBUG 标志时(即,在每个文件中),检查现有定义:

#ifndef DEBUG
#define DEBUG 1
#endif

Then, when you use the option -DDEBUG=0 with your compiler, the #define line will never be executed.然后,当您在编译器中使用选项 -DDEBUG=0 时,将永远不会执行 #define 行。

Try this instead.试试这个。

In the first file you have that will be included:在您拥有的第一个文件中,将包含:

#define DEBUG

Then whenever you want to have debug code, do this:然后,每当您想要调试代码时,请执行以下操作:

#ifdef DEBUG
do some stuff
#endif

This will also prevent your debugging code from making it into release code.这也将阻止您的调试代码使其成为发布代码。

I personally like我个人喜欢

#ifdef DEBUG
#define IFDEBUG if(0)else
#else
#define IFDEBUG if(1)else
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM