2017-06-14 52 views
0

上下文:长时间的程序员,回到C作为一个孩子。对于您可能会遇到的问题表示歉意。C:应用宏,不编译

我可以将宏应用于C程序而无需编译它吗?

我了解C宏的:他们前处理您的源文件交给受编译

:这是我的文件
编译:OK,让我申请的宏得到我可以编译的东西
编译器:好的,应用宏,让我们编译这个

有没有办法查看真正为编译而传递的程序文件?即一种应用宏的方式,但是而不是编译该程序。

例如,我有一个使用宏的PRIxPTR的小程序。

#include <stdio.h> 
#include <stdint.h> 

//needed for the PRI*PTR Macros 
#include <inttypes.h> 

int main() 
{ 
    int i = 42; 
    int* pI = &i; 

    printf("iP points to address (base 16): %" PRIxPTR "\n", (uintptr_t) pI);   
    printf("iP points to address (base 10): %" PRIdPTR "\n", (uintptr_t) pI);     
} 

编译和运行程序

$ cc main.c; ./a.out 

产生以下输出

iP points to address (base 16): 7fff545f587c 
iP points to address (base 10): 140734608922748  

我想看到C源,其PRIxPTR宏实际上产生。

这似乎是可能的 - 是吗?如果不是,我对宏的理解是不正确的?或者有什么可以防止这种情况发生?

+1

的https://stackoverflow.com/questions/985403/seeing-expanded-c-macros – Zakir

+1

至少有些IDE可以扩大个人宏调用Dup的对你也是。我知道Eclipse/CDT将指针悬停在一个上时会执行此操作。请注意,如果IDE环境和选项与编译器的环境和选项不同,则扩展可能会有所不同。 –

+1

“我可以在不编译它的情况下将宏应用于C程序吗?” - 当然。 C ** pre **处理器可以在编译器之前运行。 – Olaf

回答

3

如果您使用的是gcc,请使用-E选项。这将生成stdout的预处理器输出。

gcc -E -o src_pp.c src.c 

src_pp.c的内容:

# 1 "src.c" 
# 1 "<built-in>" 
# 1 "<command-line>" 
# 1 "/usr/include/stdc-predef.h" 1 3 4 
# 1 "<command-line>" 2 
# 1 "src.c" 
# 1 "/usr/include/stdio.h" 1 3 4 

... 

# 6 "src.c" 2 

int main() 
{ 
    int i = 42; 
    int* pI = &i; 

    printf("iP points to address (base 16): %" "l" "x" "\n", (uintptr_t) pI); 
    printf("iP points to address (base 10): %" "l" "d" "\n", (uintptr_t) pI); 
} 
+1

如果您的编译器不提供此功能,您可以尝试查找宏定义,然后手动进行文本替换。这在一些情况下很容易。 –

+0

谢谢@dbush!我会把这个标记为最好的,但这个问题已经被封闭了。 –

+0

@AlanStorm我认为你仍然可以接受15分钟过去了。 – dbush