linux c编程的debug宏

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: [email protected]
> 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 %sn",__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__:可变参数类型。

代码下载

About me

forthebadge

Creative Commons License This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。