简体   繁体   English

C:`打开调试消息

[英]C: `Turn on debug messages

This is probably a really stupid question, but how do I turn on these debug messages in my code?这可能是一个非常愚蠢的问题,但是如何在我的代码中打开这些调试消息?

#ifdef DEBUG_MSG
    printf("initial state : %d\n", initial_state);
#endif

Many thanks in advance,提前谢谢了,

When compiling, try something like this:编译时,请尝试以下操作:

$ gcc -DDEBUG_MSG -o foo foo.c

You would have to #define that somehow.你必须以某种方式#define

0. In your code. 0. 在您的代码中。

Directly in your code somewhere before you use that flag:在您使用该标志之前,直接在您的代码中的某处:

#define DEBUG_MSG

1. On the command line. 1. 在命令行上。

For each sourcefile, or appropriately in your makefile:对于每个源文件,或适当地在您的 makefile 中:

gcc -DDEBUG_MSG main.c

(For gcc, the flag is -D<macro-name> , for MSVC, it is /D , for ICC it is one of the former, depending on your operating system. ) (对于 gcc,标志是-D<macro-name> ,对于 MSVC,它是/D ,对于 ICC 它是前者之一,具体取决于您的操作系统。)

2. In your IDE, somewhere. 2. 在您的 IDE 某处。

In your IDE's project settings, find where you can put definitions.在 IDE 的项目设置中,找到可以放置定义的位置。 Under the hood, this is done using 1.在引擎盖下,这是使用 1 完成的。

#ifdef means 'If defined', your code essentially tells the preprocessor to check if DEBUG_MSG is defined somewhere else. #ifdef表示“如果已定义”,您的代码本质上是告诉预处理器检查DEBUG_MSG是否在其他地方定义。 If it is, it will include the code you've shown.如果是,它将包含您显示的代码。

The C preprocessor phase will only pass code inside an #ifdef/#endif to the compiler phase if the symbol is defined.如果定义了符号,则 C 预处理器阶段只会将#ifdef/#endif内的代码传递给编译器阶段。

You can generally do this in (at least) two ways.您通常可以通过(至少)两种方式做到这一点。

The first is to use a command line switch for the compiler such as:第一种是为编译器使用命令行开关,例如:

gcc -DDEBUG_MSG myprog.c

( -D means to define the pre-processor symbol following it and, although this is implementation-specific, many compilers use the same switch). -D表示定义其后的预处理器符号,尽管这是特定于实现的,但许多编译器使用相同的开关)。 The second is to place a line like:第二个是像这样放置一行:

#define DEBUG_MSG

inside your actual source code somewhere before the #ifdef .在您的实际源代码中#ifdef之前的某处。

The former is usually preferred since it allows you to control that behaviour without having to make changes to your source code so that, for example, you can have a debug and release build generated from the same source code.前者通常是首选,因为它允许您控制该行为而无需更改源代码,例如,您可以从相同的源代码生成调试和发布版本。

#ifdef will make your macro to be expanded only if DEBUG_MSG is defined. #ifdef只有在定义了DEBUG_MSG时才会扩展您的宏。 You can do this in two ways.您可以通过两种方式做到这一点。 Either do a #define DEBUG_MSG 1 in your source or compile using -DDEBUG_MSG (if using gcc , there are similar flags for other compilers too)在源代码中执行#define DEBUG_MSG 1或使用-DDEBUG_MSG进行编译(如果使用gcc ,其他编译器也有类似的标志)

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

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