简体   繁体   English

如何在Linux上使用#ifdef _DEBUG?

[英]How to use #ifdef _DEBUG on Linux?

I have a problem with _DEBUG macro on Linux C++. 我在Linux C ++上遇到了_DEBUG宏的问题。 I tried to use something like this: 我试着用这样的东西:

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

But it doesn't work when I select Debug in IDE. 但是当我在IDE中选择Debug时,它不起作用。 However it worked on Windows. 但它适用于Windows。 I use Eclipse IDE for C++ coding on Linux. 我在Linux上使用Eclipse IDE进行C ++编码。

If you use cmake put the following in your your top-level CMakeLists.txt file 如果您使用cmake ,请在您的顶级CMakeLists.txt文件中添加以下内容

set_directory_properties(PROPERTIES COMPILE_DEFINITIONS_DEBUG "_DEBUG")

found on CMake Mailinglist CMake Mailinglist上找到

On windows you were probably using Visual Studio, which automatically defines the _DEBUG symbol when building the solution in Debug mode. 在Windows上,您可能正在使用Visual Studio,它在调试模式下构建解决方案时自动定义_DEBUG符号。 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. 例如,如果您通过命令行使用GCC并且想要重新使用_DEBUG符号,因为您正在处理已经建立的代码库,则可以使用命令行中的-D参数来定义它。

#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. 然而,对于eclipse cdt,你最好的选择是修改项目属性中的路径和符号属性。

#define _DEBUG放在您拥有此代码段的位置的某个位置,或者放在项目设置中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM