AnSwErYWJ's Blog

Linux C编程的DEBUG宏

字数统计: 232阅读时长: 5 min
2016/07/14

DEBUG宏用于Linux下C编程时调试使用.

实现代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*************************************************************************
> File Name: debug.c
> Author: AnSwEr
> Mail: 1045837697@qq.com
> Created Time: 2015年07月23日 星期四 18时19分48秒
************************************************************************/

#include<stdio.h>
#define DEBUG_PRINT do{}while(0)

#if defined(DEBUG_PRINT)
#define DEBUG(...)\
do{\
fprintf(stderr,"-----DEBUG-----\n");\
fprintf(stderr,"%s %s\n",__TIME__,__DATE__);\
fprintf(stderr,"%s:%d:%s():",__FILE__,__LINE__,__func__);\
fprintf(stderr,__VA_ARGS__);\
}while(0)
#endif

int main(void)
{
DEBUG("Debug successfully!\n");
return 0;
}

说明

  1. *do{}while(0):使用do{…}while(0)*构造后的宏定义不会受到大括号、分号等的影响,而且可以定义空宏而不受警告。
  2. 参数介绍:
    1
    2
    3
    4
    5
    6
    __LINE__:在源代码中插入当前源代码行号;
    __FILE__:在源文件中插入当前源文件名;
    __DATE__:在源文件中插入当前的编译日期
    __TIME__:在源文件中插入当前编译时间;
    __func__:输出函数名称,功能与_Function_相同;
    __VA_ARGS__:可变参数类型。

原文作者:AnSwErYWJ

原文链接:https://answerywj.com/2016/07/14/debug-macro/

发表日期:2016/07/14 14:07

版权声明:本文采用Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License进行许可.
Creative Commons License

CATALOG
  1. 1. 实现代码
  2. 2. 说明