C/C++宏调试

调试文件test.c,代码如下:

#include <stdlib.h>
#include <stdio.h>

#define MACRO1(x) (++(x))
#define MACRO2(x) (MACRO1(x)+100)
#define MACRO3(x) (MACRO2(x)+200)

int main(void)
{
    int a = 0;
    int b = 0;

    b = MACRO3(a);

    printf("%d\n", b);

    return 0;
}

方法1、宏在预处理过程被展开,执行预处理,生产展开后的文件

             gcc -E test.c > pre.txt

方法2、宏在预处理过程被展开,执行预处理,生产展开后的文件

             gcc -g3 test.c

             使用gdb调试,然后使用命令:macro expand MACRO3(5)

            显示:expands to: (((++(5))+100)+200)

方法3、将宏作为字符串打印出来

#define macro_to_str1(x) #x
#define macro_to_str(x) macro_to_str1(x)
....
const char* str=macro_to_str(AnyMacro);
printf("%s",str);

猜你喜欢

转载自blog.csdn.net/piaopiaopiaopiaopiao/article/details/84250421