標頭檔 防止重複包含
================
根據#define 和條件編譯 #ifdef、#ifndef #else #endif 最經常的使用是 標頭檔 防止重複包含。
但是 使用
#pragma once
更好,現在 gcc cl.exe 都支援,它不但代碼更少,而且不用為宏起名字,更好。
#pragma 說明編譯器資訊//用法複雜,參考http://baike.baidu.com/view/1451188.htm
#warning 顯示編譯警告資訊
#error 顯示編譯錯誤資訊
可以在這些條件編譯中 給出某條件下的 提醒。比如 要求定義的宏沒有定義,就#error,停止編譯。
條件編譯 中的多個條件,多個宏同時判斷
====================
還有 條件編譯中 ,比如 同時判斷倆條件的
#ifdef ( a && b)該怎麼寫呢?如果是ifdef 或者 ifndef 就不能這麼寫了。
得用#if (a==1 && b==2)這種寫法了。試著去掉 #define b 2,條件裡面的 編譯資訊 和 輸出 就不會顯示了。
#include <stdio.h>#include <iostream>using namespace std;int main(int argc,char **argv){ cout<<"main"<<endl; #define a 1 #define b 2// #ifdef a && #ifdef b #if (a==1 && b==2 ) #pragma message("\npragma message YES Defined a b\n") cout<<"YES Defined a and b??"<<endl; #endif return 0;}
剛發現 ifdef也是可以多個條件的:
類似:#if defined _DEBUG || defined VLD_FORCE_ENABLE
//剛從boost-jam-3.1.18 的源碼(sf.net連結)中的pwd.c 中看到的,積累下。//看來cl貌似還定義了 NT 變數,GNU GCC定義了 __GNUC__/* MinGW on windows declares PATH_MAX in limits.h */#if defined(NT) && ! defined(__GNUC__) #include <direct.h> #define PATH_MAX _MAX_PATH#else #include <unistd.h> #if defined(__COMO__) #include <linux/limits.h> #endif#endif
VLD是一個 檢測 記憶體流失的編程工具包。值得推薦!以前遇到段錯誤,或者記憶體沒有合理的釋放掉的情況,也不知道該怎麼辦。用這個 好點。段錯誤 貌似 無法有效檢測呢。。野指標!