gcc - 查看预处理(宏展开)之后的实际代码

前言

有些使用模板或复杂宏的程序,如果想知道最终的预处理代码是啥。最好的方法就是用编译器的预处理命令,先将实现文件进行预处理,另存成实现文件,先看看。
在看AT&T汇编的书时,讲了用gcc怎样看预处理文件,很实用。

实验

测试用的源码

lostspeed@debian8:/home/dev/old_linux/gcc_flat_binary$ cat ./test.c
// @file test.c
// @brief test gen flat binary file by gcc

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

#define MAGIC_NUM 20180221 

int main(int argc, char** argv)
{
        printf("hello gcc %d\n", MAGIC_NUM);
        return EXIT_SUCCESS;
}

用gcc对测试用源码进行预处理(不编译)

lostspeed@debian8:/home/dev/old_linux/gcc_flat_binary$ gcc -E ./test.c -o my_pre.log
lostspeed@debian8:/home/dev/old_linux/gcc_flat_binary$ ls -l
总用量 56
-rw-r--r-- 1 lostspeed lostspeed 50949 1月  15 13:34 my_pre.log
-rw-r--r-- 1 lostspeed lostspeed   246 1月  15 13:17 test.c

查看预处理后的实现文件

lostspeed@debian8:/home/dev/old_linux/gcc_flat_binary$ cat ./my_pre.log
...
# 7 "./test.c" 2



int main(int argc, char** argv)
{
 printf("hello gcc %d\n", 20180221);
 return 0;
}

看预处理之后的实现文件,到底是怎么进行宏替换的,看的很清楚。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值