Where is DEBUG defined in code?

I looked at the sample code for creating a log system on my server ... And I found this

#if DEBUG printf("something here"); #endif 

I know what he is doing. This means something only if DEBUG has been defined. But where is DEBUG defined? I looked through all the header files, but I could not find DEBUG ..

Also, could you please provide me a good example or tutorial on designing a registration system?

Thanks in advance.

+4
source share
4 answers

Compilers, at least those I know, have the ability to define preprossessor macros from the "outside" of compiled files.

For example, to determine DEBUG using the microsoft compiler, you have to go with something like

 cl -DDEBUG file.cpp ... 
+4
source

Do not use a DEBUG definition that is not defined by the C ++ standard. The C ++ standard defines NDEBUG (without DEBUG), which is used for the standard assert macro, and your logging code will go hand in hand with it. DEBUG is compiler dependent. Therefore, NDEBUG provided with the standard for proper installation. Applying it to your example, use:

 #ifndef NDEBUG printf("something here"); #endif 

But my opinion is this: you should not create a log library around the NDEBUG / DEBUG pair. The logging lib should always be there, only so that you can track the behavior of the application without having to recompile the code, which in turn implies a new deployment of your code and the ability to defer error-prone behavior. The following DDJ article by Petru Marginean on the design of logging libraries describes how to provide this fact in a very efficient way in C ++:

Part 1: http://drdobbs.com/cpp/201804215

Part 2: http://drdobbs.com/cpp/221900468

For the log library, see the Boost Logging library at:

http://boost-log.sourceforge.net/libs/log/doc/html/index.html


I was blocked because NDEBUG is said to not install without explicitly defining it on the command line. I agree with this fact, but on the other hand, I understand this question here, so compiling in debug mode should also generate log output. This fact will be better applied when associating behavior with the presence of NDEBUG .

+4
source

An easy way to determine the position of the first definition is to add #define.

 #include "someone.h" #define DEBUG "dummy" // add this #if DEBUG printf("something here"); #endif 

You'll get

 foo.c:2:0: warning: "DEBUG" redefined <command-line>:0:0: note: this is the location of the previous definition 

or

 foo.c:30:0: warning: "DEBUG" redefined someone.hc:2:0: note: this is the location of the previous definition 

Another answer, try using ctags. run ctasg -R at the top of the project directory, run vim /path/to/your/code.c . move the cursor to DEBUG , then type CTRL-] .

So you can find several definitions. You can find everything with :tselect on DEBUG .

+2
source

In Visual Studio, you can set preprocessor characters in project properties. Regarding the logging system, look at log4cpp

+1
source

All Articles