How to use #ifdef _DEBUG on Linux?
Solution 1
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.
Solution 2
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
![antpetr89](https://i.stack.imgur.com/Baswo.jpg?s=256&g=1)
antpetr89
Updated on June 04, 2022Comments
-
antpetr89 about 2 years
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.