1

I'd like to be able to put some macro commands in my C++ code running NetBeans (and GCC) eg.

#ifdef DEBUG
std::cout << "DistributionSuper constructor called" << std::endl;
#endif

Does anyone know if there's a way to use a debug compilation flag in NetBeans without having to do a -DDEBUG defines on the gcc command line? Checking the g++ compile lines didn't point to any defines I could use specific to debug, but I was hoping NetBeans might have a compile variable that does this somewhere. NetBeans does have some script variable like ${CND_CONF} that might help, but I can't really see a way to get at these in the C++ code. Thanks guys Pete

Pete855217
  • 1,570
  • 5
  • 23
  • 35
  • It is a common convention for compilers to define `NDEBUG` if *not* in debug mode. I don't know about Netbeans, but maybe `#ifndef NDEBUG` does what you want? – lethal-guitar Apr 09 '13 at 16:26
  • No go lethal-guitar, it seems it's not defined. The -g option is set on the command line, I guess an idea would be to pick this up in the code, but looks maybe un-doable. No biggy, setting -DDEBUG is not too hard. – Pete855217 Apr 09 '13 at 16:34
  • Right, it is only defined if _not_ in debug mode. So if it is _not_ defined, then you are in debug mode, I'd think? – lethal-guitar Apr 09 '13 at 16:43
  • Yeah, checked both Release and Debug - it seems the flag is undefined in both situations. I'll fiddle around and write back if I find any new info on this. Thanks for the suggestion. – Pete855217 Apr 09 '13 at 16:49
  • Ah, I see. I assumed Netbeans would define it as well, but apparently it doesn't.. – lethal-guitar Apr 09 '13 at 16:52
  • @lethal-guitar `NDEBUG` isn't normally defined automatically, as far as I know, it's not linked with including or not including debug info. Note that `-g` is not "debug build", it's "include debug info", which may well be included in "release build" too (and then usually stripped later). Debug build could perhaps be defined as using `-g -O0` and perhaps a leaving out a few other flags which might be in release build. – hyde Apr 09 '13 at 19:07
  • @Pete855217 Yep, it needs to be an explicit define. Look at compile command in raw compile output and see if there's some debug define there already, which might depend on frameworks and build system used. If there is not, then you need to add something yourself. – hyde Apr 09 '13 at 19:11
  • @hyde - thanks, checked the command line, there's a few flags, but apart from -g, aren't different across release and debug compiles. Back to -DDEBUG! – Pete855217 Apr 10 '13 at 02:10

1 Answers1

3

After hunting around, couldn't find anything that would let you check debug status in the code with NetBeans compiles. Easiest way to solve the problem seems to be to do an explicity -DDEBUG in the project options, then use:

#ifdef DEBUG
    #warning In debug mode
    std::cout << "I was compiled with a DEBUG define in the g++ command line
#endif
Pete855217
  • 1,570
  • 5
  • 23
  • 35
  • I know this is an old post, I just wanted to say thanks. I am using this for my C++ netbeans project. – netcat Aug 23 '17 at 19:30
  • Seems like this is till an issue in NetBeans 11.1 :| You would think they would use `NDEBUG` given that it's apparently part of the standards (see: https://stackoverflow.com/a/29253284/2694511). – Spencer D Jul 26 '19 at 17:49
  • Yep @Spencer D, it would be nice to do a NetBeans specific debug test out of the box, but perhaps NetBeans has a strategy of keeping within GCC 'standards' in this case: so, looks like easiest way is just to a standard -DDEBUG. – Pete855217 Dec 26 '19 at 10:48