0

I am using cygwin to compile my program. The command I use is g++ -std=c++11 -W -Wall -pedantic a1.cpp

I want to execute a part of code if debug mode is defined and another part if not.

My question is, what is that command to compile in debug mode and what should I put inside my code for the if/else execution?

HsuanWei Fu
  • 91
  • 1
  • 1
  • 9

2 Answers2

0

You could add a command line option define like -DDEBUGMODE to your debug builds.

In your code you could decide what to do depending on DEBUGMODE being defined or not.

#ifdef DEBUGMODE
    //DEBUG code
#else
    //RELEASE code
#endif

I would also recommend to read _DEBUG vs NDEBUG and Where does the -DNDEBUG normally come from?

Community
  • 1
  • 1
Simon Kraemer
  • 5,700
  • 1
  • 19
  • 49
0

1- First debugging enable/disable method is to add -D to your completion command, like this:

gcc -D DEBUG <prog.c>

2- Second: To enable debugging define the MACRO that is replaced with a debugging statement like this:

#define DEBUG(fmt, ...) fprintf(stderr, fmt,__VA_ARGS__);

To disable debugging define The MACRO to be replaced with nothing like this:

#define DEBUG(fmt, ...)

Example ready to be debugged :

#include <stdio.h>
#include <stdlib.h>

int addNums(int a, int b)
{
DEBUG ("add the numbers: %d and %d\n", a,b)
return a+b;
}

int main(int argc, char *argv[])
{
int arg1 =0, arg2 = 0 ;

if (argc > 1)
  arg1 = atoi(argv[1]);
  DEBUG ("The first argument is : %d\n", arg1)

if (argc == 3)
  arg2 = atoi(argv[2]);
  DEBUG ("The second argument is : %d\n", arg2)


printf("The sum of the numbers is %d\n", addNums(arg1,arg2) );

return (0);
}
Amjad
  • 3,110
  • 2
  • 20
  • 19