天天看点

利用 __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++程序时该标识符被定义。