0
#ifdef _DEBUG
a = "ram";
#else
a = "sam";
#endif

It is working fine in Visual Studio as _DEBUG is defined in pre-processor, whereas it is not working in Linux even though I gave "-g" option while compiling. What would be the alternative for this? I want the same piece of code for both platforms.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Santhosh Kumar
  • 167
  • 2
  • 12
  • 2
    Doesn't `-D_DEBUG` option work? – MikeCAT Sep 22 '15 at 05:50
  • 7
    The only relevant macro required by the standard is `NDEBUG`. – Cheers and hth. - Alf Sep 22 '15 at 05:52
  • Why do you want your code to work differently when debugging? – melpomene Sep 22 '15 at 06:01
  • 2
    `-g` doesn't do anything special other than also generate debugging information, which is trivially removable using `strip`. – Ignacio Vazquez-Abrams Sep 22 '15 at 06:16
  • Roughly speaking, using a platform-specific feature is going to make your code at best semi-portable. There is no analogue to `_DEBUG` outside the MSVS environment; trying to use it means you will have to simulate it. For example, you could add `-D_DEBUG` to the command line options, or put `#define _DEBUG` in a universally used header, or some other trick. And you have to manage when it is set or not set. You can wish it were otherwise as much as you like, but the non-MSVS compilation systems won't change by miracle. – Jonathan Leffler Sep 22 '15 at 06:37
  • -g adds debug *information* to the build, it does not set any preprocessor variables. Do "-g -D_DEBUG" to set this as well. – jcoder Sep 22 '15 at 07:11
  • @jcoder: I think where the asker is confused is that (I believe) MS's compilers add special sauce to the compiled binary that only make it work with other debug stuff whereas GCC only adds the easily-removed information, leaving the rest of the binary as it would be were the debug option not specified. – Ignacio Vazquez-Abrams Sep 22 '15 at 10:11
  • @melpomene: i have two severs which will be built in both Debug & Release mode. using this macro i have to choose one. – Santhosh Kumar Sep 22 '15 at 10:42

1 Answers1

2

The standard macro for this is NDEBUG, though it has the opposite sense of _DEBUG (the N is for "not"). So you can write your code portably like this:

#ifndef NDEBUG
a = "ram";
#else
a = "sam";
#endif

Note how I used #ifndef to negate the conditional. Of course you could write it equivalently as:

#ifdef NDEBUG
a = "sam";
#else
a = "ram";
#endif

If you don't like this, you can also define _DEBUG on Linux by compiling like this:

g++ -g -D_DEBUG ...
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Thanks man.. it is working :) can u please tell me where NDEBUG is defined?? – Santhosh Kumar Sep 22 '15 at 10:38
  • It's usually defined by the build system. See here: http://stackoverflow.com/questions/1878645/where-does-the-dndebug-normally-come-from If you write your Makefiles by hand, you may need to define it in your release builds, similar to my `g++` command above. If you use a tool like CMake, it's done for you. – John Zwinck Sep 22 '15 at 14:23