#ifdef DEBUG与#if DEBUG [英] #ifdef DEBUG versus #if DEBUG

查看:147
本文介绍了#ifdef DEBUG与#if DEBUG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用编译器指令时,我不清楚以下两个代码片段中哪些是正确/首选的,为什么。似乎我见过的大多数开发人员和开源项目都使用了第一个,但我也看到了第二个经常使用的项目。

It is unclear to me when using compiler directives which of the below two code snippets is correct/preferred and why. It seems that most developers and Open Source projects I've seen use the first but I have seen the second used frequently as well.

#ifdef DEBUG
[self doSomethingOnlyWhenDebugging];
#endif

VERSUS

#if DEBUG
[self doSomethingOnlyWhenDebugging];
#endif

以上哪些代码段仅适用于在调试时运行代码为什么?我的猜测是,如果将DEBUG定义为TRUE或FALSE,则第一个将运行,其中第二个将仅在定义DEBUG并设置为TRUE时运行。这是正确的吗?

Which of the above code snippets is preferable for running code only while debugging and why? My guess is that the first will run if DEBUG is defined as TRUE or FALSE where the second will run only if DEBUG is defined and set to TRUE. Is that correct?

推荐答案

你是对的。 如果 DEBUG 定义为 0 >。

You are correct. #if DEBUG will not evaluate if DEBUG is defined as 0.

至于何时使用每个,你可以坚持使用 #ifdef 来处理你只需要的东西如果存在预处理器定义,则添加代码,例如添加调试日志记录。如果你需要检查值并沿着不同的编译路径,那么我将使用 0 1 。一个很好的例子是 TARGET_IPHONE_SIMULATOR ,它总是为iOS项目定义,但只有 1 如果你是编译模拟器。

As for when to use each, you can stick to using #ifdef for anything where you only need to add code if the preprocessor definition is present, such as adding debug logging. If you need to inspect the value and go down different compilation paths, then I would use a 0 or 1. A good example of that is TARGET_IPHONE_SIMULATOR, which is always defined for an iOS project, but only 1 if you’re compiling for the simulator.

这篇关于#ifdef DEBUG与#if DEBUG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆