1、定义宏(debug.h)
#ifndef __DEBUG__H #define __DEBUG__H
#include <stdio.h> #ifdef DEBUG #define debug(fmt,args...) printf (fmt ,##args) #else #define debug(fmt,args...) #endif /* DEBUG */ #endif /* __DEBUG__H */
为何是“#define debug(fmt,args...) printf (fmt ,##args)”这种宏定义方式呢?
<1> #define debug(fmt,args) printf (fmt ,##args)
当不定参数多于1个,会提示参数参数多于agrs对应的参数
<2> #define debug(fmt,...) printf (fmt ,##args)
这种情况agrs找不到对应项
<3> #define debug(fmt,...) printf (fmt ,...)
这种情况下,不定参数在预编译时全部被替换成“...”
<4> #define debug(fmt,args...) printf (fmt ,args)
我测试了,也是可以的
<5> #define debug(fmt,args) printf (fmt ,##args)
u-boot使用的方法
2、调用程序(main.c)
#include "debug.h" //#define DEBUG int main(void) { debug("File :%s Funtion :%s Line : %d\n", __FILE__, __FUNCTION__, __LINE__ ); return 0; }
在main.c中,可以通过是否定义DEBUG决定是否使能本文件中的调试函数。
后记
本功能在gcc编译环境下好使,但在VC6.0下提示“debug(fmt,args...) ”错误。
参考网页:Linux C 中连接操作符##
debug(fmt,args...)调试,布布扣,bubuko.com
原文:http://www.cnblogs.com/amanlikethis/p/3612402.html