antpetr89
antpetr89

Reputation: 1173

How to use #ifdef _DEBUG on Linux?

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.

Upvotes: 4

Views: 13496

Answers (3)

Davide Inglima
Davide Inglima

Reputation: 3495

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.

Upvotes: 6

wartburgritter
wartburgritter

Reputation: 31

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

Upvotes: 3

Matt Joiner
Matt Joiner

Reputation: 118480

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

Upvotes: 0

Related Questions