天天看點

利用 __FILE__, __LINE__輸出debug資訊

#include <stdio.h>

#define __DEBUG__

#ifdef __DEBUG__
#define DEBUG(format,...) printf("File: "__FILE__", Line: %05d: "format"\n", __LINE__, ##__VA_ARGS__)
#else
#define DEBUG(format,...)
#endif

int main(int argc, char **argv) {
    char str[]="Hello World";
    DEBUG("A ha, check me: %s\n",str);
    printf(__FILE__);
    printf("\n%d",__LINE__);
    return 0;
}
           
IplImage* init_img;  
    IplImage*** gauss_pyr, *** dog_pyr;  
    CvMemStorage* storage;  
    CvSeq* features;  
    int octvs, i, n = 0,n0 = 0,n1 = 0,n2 = 0,n3 = 0,n4 = 0;  
    int start;  
  
    /* check arguments */  
    if( ! img )  
        fatal_error( "NULL pointer error, %s, line %d",  __FILE__, __LINE__ );  
  
    if( ! feat )  
        fatal_error( "NULL pointer error, %s, line %d",  __FILE__, __LINE__ ); 
           

先介紹幾個編譯器内置的宏定義,這些宏定義不僅可以幫助我們完成跨平台的源碼編寫,靈活使用也可以巧妙地幫我們輸出非常有用的調試資訊。

ANSI C标準中有幾個标準預定義宏(也是常用的):

__LINE__:在源代碼中插入目前源代碼行号;

__FILE__:在源檔案中插入目前源檔案名;

__DATE__:在源檔案中插入目前的編譯日期

__TIME__:在源檔案中插入目前編譯時間;

__STDC__:當要求程式嚴格遵循ANSI C标準時該辨別被指派為1;

__cplusplus:當編寫C++程式時該辨別符被定義。