Codeblocks Debug Preprocessor

I am writing a C ++ program with code blocks, and for debugging purposes I need to know if the Building-Target of Codeblocks is set to "DEBUG" or "RELEASE".

I already tried this:

#ifdef DEBUG printf("Debug-Message"); #endif 

and this one

 #ifdef _DEBUG printf("Debug-Message"); #endif 

But not one of these words is defined. Do I have to define DEBUG myself and change it every time I change the Building-Target, or is there a word that I don’t know?

+4
source share
2 answers

Do I have to define DEBUG myself and change it every time I change Building-Target, or is there a word that I don’t know?

I don't know what if something is set by default to Code :: Blocks. But, if you define your own #defines

 Project->Build options...->[Debug|Release]->#defines 

you do not need to change them when switching between assembly targets (DEBUG or RELEASE). It allows you to define values ​​specific to the Debug assembly, as well as values ​​specific to the Release assembly.

To avoid having to manually enter it each time for each new project, you can make a small project only using your Debug / Release #defines parameters and save it as a project template , and then create new projects from this project template.

+3
source

The usual way suggested by the assert (3) man page and habits (from <assert.h> in C or <cassert> in C ++) is to define NDEBUG on the command line (for example, to compile with gcc -Wall -DNDEBUG ) to compile without debugging. In the Makefile you could CPPFLAGS += -DNDEBUG in release mode (and compile with g++ -Wall -g in debug mode).

My own habit may have something like

 #ifndef NDEBUG #define dbgprintf(Fmt,...) do{fprintf(stderr,"%s:%d:" Fmt "\n", \ __FILE__, __LINE__, \ ##__VA_ARGS__);}while(0) #else #define dbgprintf(Fmt,...) do{}while(0) #endif 

in the general header file and use dbgprintf("i=%d", i) elsewhere in the code. Note that I use the constant string binding in the Fmt macro Fmt , adding a newline constant to it, and that my debug output contains the source file name and line number (you can also use __func__ if you want). In pure C ++ code, I could

 #ifndef NDEBUG #define DBGOUT(Out) do{std::out << __FILE__ << ":" << __LINE__ \ << " " << Out << std::endl;}while(0) #else #define DBGOUT(Out) do{}while(0) #endif 

and use DBGOUT("i=" << i) in order to use special operator << definitions for my types.

+3
source

Source: https://habr.com/ru/post/1440947/


All Articles