I'm writing a C++ Program with Codeblocks and for debugging-purposes I need to know if the Building-Target of Codeblocks is set to "DEBUG" or to "RELEASE".
I already tried this:
#ifdef DEBUG
printf("Debug-Message");
#endif
and this
#ifdef _DEBUG
printf("Debug-Message");
#endif
But none of this words are defined. Do I have to define DEBUG on my own and change it, everytime I change the Building-Target, or is there a word I don't know?
1) Start CodeBlocks 2) Select the Settings menu 3) Select the Debugger... option in the Settings menu 4) In the Debugger Settings window, select the Default category under GDB/CDB debugger 5) You should see a field named "Executable path:" in the Debugger settings window.
Download and Install Codeblocks Download the installer with GCC Compiler, e.g., codeblocks-20.03mingw-setup.exe (which includes MinGW's GNU GCC compiler and GNU GDB debugger). Run the downloaded installer. Accept the default options.
The debugger that you will use is part of an Open Source free IDE called Code::Blocks, which we have found easy to use and is described in these notes. Code::Blocks has a C++ editor and compiler. It will allow you to create and test your programs from one easy to use application.
In Code::Blocks, go into your Settings menu, then click Compiler . Make sure Global compiler settings is selected in the sidebar, then switch to the Toolchain executables tab. Your Debugger entry should say something like GDB/CDB debugger: default. If it does, OK out of this dialog.
Do I have to define DEBUG on my own and change it, everytime I change the Building-Target, or is there a word I don't know?
I don't know what, if anything is set by default by Code::Blocks. But, if you define your own #defines
Project->Build options...->[Debug|Release]->#defines
you don't have to change them as you switch between build targets (DEBUG or RELEASE). It lets you define values that are specific to the Debug build, as well as values that are specific to the Release build.
To avoid having to manually enter it each time for each new project you can make a small project with just your Debug/Release #defines and save that as a project template and then create new projects from that project template.
The usual way, as suggested by assert(3) man page and habits (with <assert.h>
in C or <cassert>
in C++), is to define NDEBUG
at the command line (e.g. to compile with gcc -Wall -DNDEBUG
) for non-debug compilation. In your Makefile
you could CPPFLAGS += -DNDEBUG
in release mode (and compile with g++ -Wall -g
in debug mode).
My own habit might be to 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 a common header file, and to use dbgprintf("i=%d", i)
elsewhere in the code.
Notice that I use constant string catenation on the Fmt
macro argument, that I append a constant newline to it, and that my debugging output contains the source file name and line number (you might also use __func__
if you like). In pure C++ code, I might instead have
#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)
with the advantage of using specific definitions of operator <<
for my types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With