“#if cpp”在C文件中的含义是什么?

时间:2021-12-23 16:54:56

I am working on some source code these days. In some codes, I find these at the head. Dunno what it means. Any ideas?

这些天我正在研究一些源代码。在某些代码中,我发现了这些。不知道这意味着什么。有任何想法吗?

#include "pth_p.h"

#if cpp

#ifndef PTH_DEBUG

#define pth_debug1(a1)                     /* NOP */
#define pth_debug2(a1, a2)                 /* NOP */
#define pth_debug3(a1, a2, a3)             /* NOP */
#define pth_debug4(a1, a2, a3, a4)         /* NOP */
#define pth_debug5(a1, a2, a3, a4, a5)     /* NOP */
#define pth_debug6(a1, a2, a3, a4, a5, a6) /* NOP */

#else

#define pth_debug1(a1)                     pth_debug(__FILE__, __LINE__, 1, a1)
#define pth_debug2(a1, a2)                 pth_debug(__FILE__, __LINE__, 2, a1, a2)
#define pth_debug3(a1, a2, a3)             pth_debug(__FILE__, __LINE__, 3, a1, a2, a3)
#define pth_debug4(a1, a2, a3, a4)         pth_debug(__FILE__, __LINE__, 4, a1, a2, a3, a4)
#define pth_debug5(a1, a2, a3, a4, a5)     pth_debug(__FILE__, __LINE__, 5, a1, a2, a3, a4, a5)
#define pth_debug6(a1, a2, a3, a4, a5, a6) pth_debug(__FILE__, __LINE__, 6, a1, a2, a3, a4, a5, a6)

#endif /* PTH_DEBUG */

#endif /* cpp */

3 个解决方案

#1


it is testing if some constant named cpp that is visible to the preprocessor (is likely a macro) is non-zero.

它正在测试预处理器可见的一些名为cpp的常量(很可能是一个宏)是非零的。

Usual convention is that macros should be all uppercase so that they are more obvious, but they don't have to be (as is apparently the case here).

通常的惯例是宏应该全部为大写,以便它们更明显,但它们不一定是(显然就是这里的情况)。

My guess is that it stands for c++ and one of the included headers (perhaps pth_p.h?) defines it if a c++ compiler is being used. If this is the case, there are more standard things to use which are preferred such as this:

我的猜测是它代表c ++,其中一个包含的头文件(也许是pth_p.h?)定义了它是否正在使用c ++编译器。如果是这种情况,可以使用更多标准的东西,例如:

#ifdef __cplusplus

#2


This is an example of a preprocessor directive that allows you to test an expression at compilation time. Here is a good explanation.

这是一个预处理程序指令的示例,它允许您在编译时测试表达式。这是一个很好的解释。

#3


I suspect that this is checking whether the code is being compiled by a c++ compiler or a c compiler. If C++, the macros are defined if there is also the PTH_DEBUG flag set. I suspect that this flag would be set in a call to the compiler. If C, the macros are not defined. Why that is the case depends on what pth_debug macros are intended to do.

我怀疑这是检查代码是由c ++编译器还是c编译器编译的。如果是C ++,如果还设置了PTH_DEBUG标志,则定义宏。我怀疑这个标志会在调用编译器时设置。如果是C,则未定义宏。为什么会出现这种情况取决于pth_debug宏的用途。

#1


it is testing if some constant named cpp that is visible to the preprocessor (is likely a macro) is non-zero.

它正在测试预处理器可见的一些名为cpp的常量(很可能是一个宏)是非零的。

Usual convention is that macros should be all uppercase so that they are more obvious, but they don't have to be (as is apparently the case here).

通常的惯例是宏应该全部为大写,以便它们更明显,但它们不一定是(显然就是这里的情况)。

My guess is that it stands for c++ and one of the included headers (perhaps pth_p.h?) defines it if a c++ compiler is being used. If this is the case, there are more standard things to use which are preferred such as this:

我的猜测是它代表c ++,其中一个包含的头文件(也许是pth_p.h?)定义了它是否正在使用c ++编译器。如果是这种情况,可以使用更多标准的东西,例如:

#ifdef __cplusplus

#2


This is an example of a preprocessor directive that allows you to test an expression at compilation time. Here is a good explanation.

这是一个预处理程序指令的示例,它允许您在编译时测试表达式。这是一个很好的解释。

#3


I suspect that this is checking whether the code is being compiled by a c++ compiler or a c compiler. If C++, the macros are defined if there is also the PTH_DEBUG flag set. I suspect that this flag would be set in a call to the compiler. If C, the macros are not defined. Why that is the case depends on what pth_debug macros are intended to do.

我怀疑这是检查代码是由c ++编译器还是c编译器编译的。如果是C ++,如果还设置了PTH_DEBUG标志,则定义宏。我怀疑这个标志会在调用编译器时设置。如果是C,则未定义宏。为什么会出现这种情况取决于pth_debug宏的用途。