uboot中宏開關。

#define DEBUG是什麼意思呢?

編譯器在內存中會維護一張關於 #define的表結構。這裏只是添加了一個表項叫DEBUG,沒有內容。這種寫法本身沒有錯誤,倒是下面運用到了一個技巧:#ifndef是查詢該表有沒有該表項,如果沒有即爲成功,此時將會編譯之後的程序語句。一般是爲了調試而設置的一些程序。待發布release版本時,沒有了DEBUG表項,所以很多語句就不編譯了,此時的程序被極度優化,EXE文件也小了很多倍。
 
我的理解是這樣的:
這個正如#DEFINE R 3.142
同樣理解R就代表了3.142
在定義#DEFINE DEBUG時,後面的內容是空的,因爲我的下面程序沒有出現錯誤:此段代碼定義了一個test什麼都沒定義,使用cout << test <<end;時報錯,而在使用我們認爲不可能正確的cout << test 1 << endl;時卻正確。所以test就是什麼都沒有。
#define test
#include "iostream.h"
void main()
{
cout << test 1 << endl;
}
 
#define DEBUG,定義沒有值的DEBUG主要是用於控制調試程序的運行。當定義了DEBUG時"#ifdef DEBUG" 則執行某些調試用的代碼,若把"#define DEBUG"刪除了後,"#ifdef DEBUG" 就可以使程序不執行某些代碼。
 
 
把DEBUG替換成空字符串,只是表示有DEBUG這個東西,使#if defined DEBUG 可以編譯
其它編譯器不清楚,VC下會經常出現,在VC下DEBUG和NDEBUG是微軟定義好的,
在debug版本編譯下,編譯器會默認定義了DEBUG這個宏;
在release(發佈版)版本下,編譯器會默認定義宏NDEBUG
所以最好不要自已把DEBUG和NDEBUG宏定義成其它的意思
這樣就可以利用
#ifdefDEBUG
/****做debug下要做的事*****
#else
/**做release版本下的事*******/
#endif
這樣做的好處是無論在debug下還是release下,可以用同一個代碼
 
DE>
#define DEBUG
main()
{
#ifdef DEBUG
    printf("Debugging\n");
#else
    printf("Not debugging\n");
#endif
    printf("Running\n");
}
DE>
 
在用vc時,利用AppWizard會產生如下代碼:
 
DE>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
DE>
 
對於
#define new DEBUG_NEW
首先看msdn的解釋:
 
DE>
Assists in finding memory leaks. You can use DEBUG_NEW everywhere in your program that you would ordinarily use the new operator to allocate heap storage.
 
In debug mode (when the _DEBUG symbol is defined), DEBUG_NEW keeps track of the filename and line number for each object that it allocates. Then, when you use the CMemoryState::DumpAllObjectsSincemember function, each object allocated with DEBUG_NEW is shown with the filename and line number where it was allocated.
 
To use DEBUG_NEW, insert the following directive into your source files:
 
#define new DEBUG_NEW
 
Once you insert this directive, the preprocessor will insert DEBUG_NEW wherever you use new, and MFC does the rest. When you compile a release version of your program, DEBUG_NEW resolves to a simple newoperation, and the filename and line number information is not generated.
 
DE>
 
再查看定義:
 
DE>
#ifdef _DEBUG
 
void* AFX_CDECL operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
#define DEBUG_NEW new(THIS_FILE, __LINE__)
 
#else
 
#define DEBUG_NEW new
 
#endif
DE>
 
這樣就很清楚了,當在debug模式下時,我們分配內存時的new被替換成DEBUG_NEW,而這個DEBUG_NEW不僅要傳入內存塊的大小,還要傳入源文件名和行號,這就有個好處,即當發生內存泄漏時,我們可以在調試模式下定位到該問題代碼處。若刪掉該句,就不能進行定位了。而在release版本下的new就是簡單的new,並不會傳入文件名和行號。
 
因此,我們在開發代碼階段,保留上述代碼是值得的。
 
DE>
#ifdef DEBUG
#define F_OUT                       printf("%s:", __FUNCTION__);fflush(stdout);
#define L_OUT                       printf("%s:%d:", __FILE__, __LINE__);fflush(stdout);
#define A_OUT                       printf("%s:%d:%s:", __FILE__, __LINE__, __FUNCTION__);fflush(stdout);
#define D_OUT                       printf("DEBUG:");fflush(stdout);
 
#define F_PRINTF(fmt, arg...)        F_OUT printf(fmt, ##arg)
#define L_PRINTF(fmt, arg...)        L_OUT printf(fmt, ##arg)
#define A_PRINTF(fmt, arg...)       A_OUT printf(fmt, ##arg)
#define PRINTF(fmt, arg...)            D_OUT printf(fmt, ##arg)
#define DBUG(a)     {a;}
#else
#define F_OUT
#define L_OUT
#define A_OUT
#define D_OUT
 
#define F_PRINTF(fmt, arg...)
#define L_PRINTF(fmt, arg...)
#define A_PRINTF(fmt, arg...)
#define PRINTF(fmt, arg...)
#define DBUG(a)
#endif
 
#define F_PERROR(fmt)                F_OUT perror(fmt)
#define L_PERROR(fmt)                L_OUT perror(fmt)
#define A_PERROR(fmt)               A_OUT perror(fmt)
#define PERROR(fmt)                    D_OUT perror(fmt)
DE>
 
 

博主小結:
通過取消debug定義刪除掉調試階段相關的代碼,這是開發者必備的技巧。要使用這個技巧,先明確release版本和debug版本的區別
發佈了4 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章