gcc转储解析定义 [英] gcc dump resolved defines

查看:131
本文介绍了gcc转储解析定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下代码:

I have code like

#define ONE 1
#define TWO 2
#define SUM (ONE+TWO)

如何将SUM转储为gcc中解析的值3 4.3 +?

How do I dump SUM as "3", the resolved value, in gcc 4.3+?

gcc -dM -E foo.h 似乎只是按原样转储它。我如何获得实际值,就像它插入到编译阶段一样?

gcc -dM -E foo.h only seems to dump it as is. How do I get the actual value like it's inserted on compilation stage?

推荐答案

你不能。就编译器而言,预处理之前的行 printf(%d \ n,SUM)与行 printf( %d \\\
,1 + 2)
。编译器恰好执行称为不断折叠的优化,即使在默认优化级别( -O0 ),这会在运行时将结果转换为常量。

You can't. As far as the compiler is concerned, the line printf("%d\n", SUM) before preprocessing is indistinguishable from the line printf("%d\n", 1+2). The compiler just happens to perform an optimization called constant folding, even at the default optimization level (-O0), which turns the result into a constant 3 at runtime.

没有真正的好方法可以看到输出这些优化。您可以使用 -S 选项来查看生成的汇编代码并查看看起来像什么,但是如果您的程序比玩具大,那将会是很多手动的努力。您还可以使用 -fdump-tree 选项之一查看分析树(请参阅GCC手册页)。

There's not really a good way to see the output of these optimizations. You could use the -S option to view the generated assembly code and see what that looks like, but if your program is anything larger than a toy, that will be a lot of manual effort. You could also look at the parse tree by using one of the -fdump-tree options (see the GCC man page).

这篇关于gcc转储解析定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆