關於C的先行編譯 宏定義 的一些使用[不斷積累中]

來源:互聯網
上載者:User

標頭檔 防止重複包含

================

根據#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是一個 檢測 記憶體流失的編程工具包。值得推薦!以前遇到段錯誤,或者記憶體沒有合理的釋放掉的情況,也不知道該怎麼辦。用這個 好點。段錯誤 貌似 無法有效檢測呢。。野指標!

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.