__FILE__ __LINE__ C++ 宏定义 调试

参考博文: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_“;在发布时,直接注释掉该宏,则不会输出调试信息,非常方便。

原文地址:https://www.cnblogs.com/wenshanzh/p/2982151.html