2009-02-11 40 views
2

這幾天我正在處理一些源代碼。在一些代碼中,我發現這些代碼在頭部。不知道這意味着什麼。有任何想法嗎?「#if cpp」在C文件中是什麼意思?

#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 */ 

回答

10

它正在測試預處理器(可能是宏)是否可見的某個常量命名的cpp非零。通常的慣例是宏應該全部大寫,以便它們更明顯,但它們不一定是(這裏顯然是這種情況)。

我的猜測是它代表C++,並且包含的​​頭文件之一(可能是pth_p.h?)定義了它,如果使用C++編譯器的話。如果是這樣的情況下,有使用更標準的東西,其是優選的,例如這樣的:

#ifdef __cplusplus 
2

我懷疑,這是檢查該代碼是否是由一個C++編譯器或一個C編譯器編譯。如果C++,宏定義是否也有PTH_DEBUG標誌設置。我懷疑這個標誌會在調用編譯器時被設置。如果C,宏沒有定義。爲什麼這種情況取決於pth_debug宏的用途。