1

Is there a way to find out the preprocessor in c++ code, e.g. NDebug, NOMAXMIN, etc?

I can do something like

#ifdef _DEBUG
    std::cout << "in debug mode";
#else
    std::cout << "in release mode";
#endif

but there are so many preprocessors and colleagues can also define their own.

I'm using Microsoft Visual Studio to write C++ code, not gcc.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
athos
  • 6,120
  • 5
  • 51
  • 95
  • 1
    Do you mean all the preprocessor directives? There is a list of standard ones and many compiler vendors provide their own also. – Thomas Matthews Sep 06 '17 at 23:27
  • Do you mean that you want to produce a list of all defined macros? A *macro* is a named piece of code such as `_DEBUG`, a *preprocessor* is a software tool that evaluates macros (among other things). – Beta Sep 06 '17 at 23:28
  • Possible duplicate of [GCC dump preprocessor defines](https://stackoverflow.com/questions/2224334/gcc-dump-preprocessor-defines) – spectras Sep 07 '17 at 00:40
  • @spectras thanks reminder, i'm actually working on microsoft visual studio. – athos Sep 07 '17 at 10:39
  • @athos> in that case the full list is available in visual studio [documentation](https://msdn.microsoft.com/en-us/library/b0084kay.aspx). – spectras Sep 07 '17 at 14:18
  • @spectras That's the vs built-in preprocessors . How to figure them out so that c++ code is aware of the settings? Even better, could c++ code be aware of the programmer defined preprocessors? – athos Sep 07 '17 at 16:01
  • @athos> I don't understand. You have the full list of predefined macros, with exact meaning of each in case you need them in your code. What exactly do you miss? – spectras Sep 07 '17 at 16:29
  • How to handle a new preprocessor defined by a colleague? – athos Sep 07 '17 at 16:31
  • @athos> by asking that colleague what the meaning of the macro he defined is? We don't do mind reading. Neither do compilers. – spectras Sep 07 '17 at 16:33
  • I'm looking for an automatic solution – athos Sep 07 '17 at 16:34
  • An automatic solution to what? Extracting the meaning of a macro from a colleague's mind? – spectras Sep 07 '17 at 16:36

1 Answers1

0

Visual Studio automatically defines _DEBUG, so I suggest you use that.

However, for a portable solution, use NDEBUG, like this:

#ifdef NDEBUG
  // nondebug
#else
  // debug code
#endif

References:

  1. Predefined Macros Msdn
  2. Automatic #defines according to Debug/Release config in Visual Studio 2010
  3. C / C++ : Portable way to detect debug / release?

PS: There is no automatic way of detecting the meaning of a custom Macro defined by a colleague, since the colleague might do almost anything.

gsamaras
  • 71,951
  • 46
  • 188
  • 305