3

I have a problem with _DEBUG macro on Linux C++. I tried to use something like this:

#ifdef _DEBUG
cout << "Debug!" << endl;
#endif

But it doesn't work when I select Debug in IDE. However it worked on Windows. I use Eclipse IDE for C++ coding on Linux.

Mat
  • 202,337
  • 40
  • 393
  • 406
antpetr89
  • 1,143
  • 3
  • 12
  • 14

3 Answers3

5

On windows you were probably using Visual Studio, which automatically defines the _DEBUG symbol when building the solution in Debug mode. Symbols are usually defined when invoking the compiler, and then they will be used during the pre-processor phase.

For example, if you are using GCC via command line and you want to re-use the _DEBUG symbol because you are handling an already established codebase, you can define it using the -D parameter via command line.

#include <iostream>
int main() {
  std::cout << "Hello world!\n";
#ifdef _DEBUG
  std::cout << "But also Hello Stack Overflow!\n";
#endif
  return 0;
}

Compiling this with the following command line

 g++ -D _DEBUG define.cpp

will give the following result

Hello world!
But also Hello Stack Overflow!

Your best bet for eclipse cdt however could be to modify the paths and symbols properties in the project properties.

Davide Inglima
  • 3,455
  • 1
  • 18
  • 9
3

If you use cmake put the following in your your top-level CMakeLists.txt file

set_directory_properties(PROPERTIES COMPILE_DEFINITIONS_DEBUG "_DEBUG")

found on CMake Mailinglist

Runcorn
  • 5,144
  • 5
  • 34
  • 52
0

Put #define _DEBUG somewhere north of where you have this snippet, or in your project settings.

Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
  • I highly recommend not having it in any c++ file. Project settings is fine, but not in the code itself. – Mooing Duck Jan 27 '12 at 16:01
  • @MooingDuck: Yeah that's true. Personally I just wouldn't use MSVC, or STL where this is heavily used, and stick to NDEBUG which is in common use. – Matt Joiner Jan 28 '12 at 03:26
  • @user1173593: You want `#define _DEBUG`... Read some of the MSVC doco for your compiler, or your STL collection. – Matt Joiner Mar 20 '12 at 14:32