如何在 Linux 上使用 #ifdef _DEBUG?
我在 Linux C++ 上遇到 _DEBUG 宏问题。 我尝试使用这样的方法:
#ifdef _DEBUG
cout << "Debug!" << endl;
#endif
但是当我在 IDE 中选择“调试”时,它不起作用。然而它在 Windows 上运行。 我在 Linux 上使用 Eclipse IDE 进行 C++ 编码。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Windows 上,您可能使用 Visual Studio,它在调试模式下构建解决方案时自动定义 _DEBUG 符号。符号通常在调用编译器时定义,然后在预处理器阶段使用它们。
例如,如果您通过命令行使用 GCC,并且由于正在处理已建立的代码库而希望重复使用 _DEBUG 符号,则可以通过命令行使用 -D 参数来定义它。
使用以下命令行进行编译
将给出以下结果
对于 eclipse cdt,您的最佳选择可能是 修改项目属性中的路径和符号属性。
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.
Compiling this with the following command line
will give the following result
Your best bet for eclipse cdt however could be to modify the paths and symbols properties in the project properties.
CMakeLists.txt 文件中
如果您使用cmake,请将以下内容放入CMake 邮件列表
If you use cmake put the following in your your top-level CMakeLists.txt file
found on CMake Mailinglist
将
#define _DEBUG
放置在您拥有此代码段的位置以北的某个位置,或者放在您的项目设置中。Put
#define _DEBUG
somewhere north of where you have this snippet, or in your project settings.