debug宏起作用应用

在linux内核中重新定义了printk,如pr_debug,dev_dbg等。要使用这些宏函数就需要定义DEBUG。

详见:kernel printk信息显示级别

那么DEBUG该定义在什么地方呢?

定义到pr_debug或dev_dbg头文件中,则内核所有的宏都起作用。则打印信息太多,不易调试。

理想的宏定义 应该是想打印信息的模块或文件中,那应放到什么位置呢?

最好放到使用文件的最开头

测试用例:

//#define DEBUG

static inline int poww(int v)
{
#ifdef DEBUG
    return v*v;
#else
    return v;
#endif
}
#include <stdio.h>

#define DEBUG

#include "macro.h"

//#define DEBUG

int main()
{
    int test = 0;

    test = poww(5);

    printf("macro value is %d
", test);

    return 0;
}

注:上述不同位置定义DEBUG,输出结果不同。

要使DEBUG宏起作用,应将DEBUG定义到宏函数头文件的前面,最不易出错的位置为文件开头。

宏在程序编译的预处理阶段,直接在代码处展开,且按宏定义处当时值带入文件中,所以在宏定义头文件的前和后定义DEBUG结果完全不一样。

原文地址:https://www.cnblogs.com/embedded-linux/p/6561847.html