看板 C_and_CPP 關於我們 聯絡資訊
在網路上找資料時 看到下面這個範例 // ------------------ #ifndef __INCvxWorksh #define __INCvxWorksh // ------------------ #ifdef __cplusplus extern "C" { #endif /*...*/ #ifdef __cplusplus } #endif #endif /* __INCvxWorksh */ 其中我自己在看code的時候也常常會看到 但是不太懂它的意義 有看到如下說明 #ifndef __INCvxWorksh、#define __INCvxWorksh、#endif” 的作用是 防止該標頭檔被重複引用。 但是還是不懂 因為不管 __INCvxWorksh 有沒有被 def 下面的程式碼應該都會被編譯一次 那到底這樣寫有什麼用 或在什麼情況下有用 有例子嗎? 謝謝大家 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 119.14.173.206 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1449064395.A.D5D.html
stupid0319: 寫Driver? 12/02 22:04
jfcherng: if (not define A) { 12/02 22:05
jfcherng: define A; 12/02 22:05
jfcherng: // other codes 12/02 22:05
jfcherng: } // #endif 12/02 22:05
jfcherng: 類似這樣被解釋的吧 12/02 22:06
Laleh: 那跟 #pragma once 有差異嗎 thx~ 12/02 22:10
stupid0319: 說不是有其他標頭檔有相同的#define __INCvxWorksh 12/02 22:21
scwg: http://lmgtfy.com/?q=include+guard 12/02 23:25
chuegou: 是指編譯過程中遇到第二次#include "INCvxWorks.h" 12/03 00:32
chuegou: 就不會重複編譯吧 12/03 00:33
overhead: 很多檔案include來include去時,.h檔常常會被重複編譯 12/03 00:34
overhead: 你應該是沒有注意到檔案有被重複編譯的可能性吧? 12/03 00:35
longlongint: 你知道 Macro 嗎 12/03 06:00
OPIV: 和 #pragma once 是一樣的 12/03 08:37
IhateOGC: 你知道重複宣告嗎? 12/08 13:14