1

Im writing a small project in C++ and im supposed to include parts in code that would run only under the condition _DEBUG. Code looks like that:

#ifdef _DEBUG 
//Debuging code area
#endif

Visual Studio supports it by default, just by clicking "Run" or "Debug" inside IDE. How to use the same function in other enviroments? Is it posible to do it in Clion (Clang compiler)? How?

Jacek
  • 171
  • 2
  • 12
  • The standard one is `NDEBUG` for no debug. You'll find this is what `assert` uses. Anyway, compilers and IDEs allow you to define whatever you want. There should be documentation on that. – chris Mar 17 '17 at 12:47

2 Answers2

2

A more common #define is NDEBUG for non debug builds (i.e. assert() is disabled if NDEBUG is defined, see reference here).

In Visual Studio projects the _DEBUG marco is #defined by default for Debug targets, but you can remove it from the list of preprocessor definitions in the project properties dialog.

On other platforms, just pass the #define to the compiler in some way, i.e. for gcc compiler on *nix systems, just use the -D command line option:

gcc -D _DEBUG ....

(reference here)

You can find some more info about _DEBUG vs NDEBUG in this other StackOverflow post.

Community
  • 1
  • 1
roalz
  • 2,699
  • 3
  • 25
  • 42
1

When developing software for cross-platform compatibility, I usually prefer to explicitly pass the #define of _DEBUG to the compiler/toolchain.

Microsoft Visual C++ compiler already #defines it, but for GCC you can pass it using the -D commandline options, i.e.:

gcc -D _DEBUG ...

In case you use CMake as building tool (as I do), you can more easily add it to your CMakeLists.txt file:

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_DEBUG")