您的位置:首页 > 编程语言 > C语言/C++

__FILE__ __LINE__ C++ 宏定义 调试

2013-03-26 11:23 204 查看
参考博文:http://www.cnblogs.com/lixiaohui-ambition/archive/2012/08/21/2649052.html

自定义宏定义,来控制printf输出调试信息。

1)使用可变参数的宏 ##__VA_ARGS__ 来对应printf的多参数。(注意:其中 __ 为2个连续的”_“)

2) 使用编译器内置的宏 __FILE__ __LINE__ 来输出代码所在文件名及行号(注意:其中 __ 为2个连续的”_“)

代码示例如下:

//================================================
// Name : debug.cpp
// Author : vin
// Version : 1.0
// Description : Hello World in C++, Ansi-style
//================================================

#include <stdio.h>
#include <stdlib.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()
{
char str[] = "Hello World";
DEBUG("A ha,check me :%s", str);
// printf("hello\n");
system("pause");
return 0;
}


在代码调试阶段,保留 ”#define _DEBUG_“;在发布时,直接注释掉该宏,则不会输出调试信息,非常方便。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: