简体   繁体   English

在Linux上替代_DEBUG

[英]Alternative to _DEBUG on Linux

#ifdef _DEBUG
a = "ram";
#else
a = "sam";
#endif

It is working fine in Visual Studio as _DEBUG is defined in pre-processor, whereas it is not working in Linux even though I gave "-g" option while compiling. 它在Visual Studio中可以正常工作,因为_DEBUG是在预处理器中定义的,但是即使我在编译时提供了“ -g”选项,它也不能在Linux中工作。 What would be the alternative for this? 有什么替代方法呢? I want the same piece of code for both platforms. 我想要两个平台使用相同的代码。

The standard macro for this is NDEBUG , though it has the opposite sense of _DEBUG (the N is for "not"). 对此的标准宏是NDEBUG ,尽管它具有_DEBUG的相反含义( N表示“ not”)。 So you can write your code portably like this: 因此,您可以像下面这样方便地编写代码:

#ifndef NDEBUG
a = "ram";
#else
a = "sam";
#endif

Note how I used #ifndef to negate the conditional. 注意我如何使用#ifndef否定条件。 Of course you could write it equivalently as: 当然,您可以等效地将其编写为:

#ifdef NDEBUG
a = "sam";
#else
a = "ram";
#endif

If you don't like this, you can also define _DEBUG on Linux by compiling like this: 如果您不喜欢这样,还可以通过以下编译在Linux上定义_DEBUG

g++ -g -D_DEBUG ...

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

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