确定C宏的扩展 [英] Determine the expansion of a C macro

查看:36
本文介绍了确定C宏的扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有办法通过检查已编译的对象或在.c或.h文件中运行某种形式的gcc -E来确定扩展的C宏的最终值"?

Is there a way to determine the "final value" of a expanded C macro by inspecting the compiled object or by running some form of gcc -E on the .c or .h file?

test.h

#define AAA 1
#define BBB 10
#define CCC (AAA+BBB)

test.c

#include <stdio.h>
#include "test.h"

int main(){
   printf("%d\n", CCC);
   return 0;
}

因此有某种方法可以获取扩展值:

Thus is there someway to get the expanded value either:

#define CCC 11

#define CCC (1+10)

如果使用编译

gcc -dM -E test.c | grep CCC 

gcc -dD -E test.c | grep CCC

输出

#define CCC (AAA+BBB)

这要求我仍然知道 AAA BBB 是什么.

which requires me to still know what AAA and BBB are.

编译:

gcc -E test.c

给予(跳过样板):

# 4 "test.c"
int main(){
printf("%d\n", (1+10));
return 0;
}   

虽然扩展了 CCC ,但是现在我已经失去了映射回 CCC 的映射.

While its expanded CCC I've now lost the mapping back to CCC.

尚不清楚,我想以某种方式确定CCC是什么(11或1 + 10(如gcc -E示例所示,它仅插入(1 + 10)而不是11)),最好不更改代码本身.Printf是在MRE中使用的一个坏主意,我实际上想到的是这样的代码:

As it was unclear, what i'd like is someway to determine what CCC is (either 11 or 1+10 (as the gcc -E example showed it only inserted (1+10) not 11), preferably without altering the code itself. Printf was a bad idea to use in the MRE, what i actually had in mind was code like:

struct my_struct {
    int my_array[CCC]
    ... other stuff ...
}

问题是my_array有多大,所以我可以用另一种语言(通过ctypes通过python)构造一个结构,并知道我需要多少空间.我知道我可以使用pahole的结构,但希望仅使用gcc和更一般的情况(例如,不在结构中的全局数组)来做到这一点.

The question is how big is my_array, so i can make a struct in another language (python via ctypes) and know how much space i need. I know for structs i can use pahole but was hoping to do this with only gcc and in the more general case (say a global array not in a struct).

推荐答案

预处理器永远不会创建

#define CCC (1+10)

CCC 的扩展名始终为(AAA + BBB);只是重新扫描了宏扩展的结果以扩展更多宏,这时 AAA BBB 变成了 1 10 .

The expansion of CCC is always (AAA+BBB); it's just that the result of macro expansion is rescanned for more macros to expand, at which point AAA and BBB turn into 1 and 10, respectively.

也许更清楚的例子是

#define AAA 1
#define CCC AAA
#define AAA "hello"

size_t x = sizeof CCC;

此代码将扩展为"hello" ,而不是 1 . CCC 的值始终为 AAA ;只是到 size_t x = sizeof CCC; 处理时, AAA 本身就会变成"hello" .

This code will expand to "hello", not 1. CCC always has a value of AAA; it's just that by the time size_t x = sizeof CCC; is processed, AAA itself will turn into "hello".

此示例还演示了可以重新定义宏的原因,因此甚至可能没有一个单独的答案" CCC 的值是什么?".

This example also demonstrates that macros can be redefined, so there may not even be a single answer to "what is the value of CCC?".

这就是为什么没有简单的编译器调用或切换的原因;您想要的东西根本不存在.

That's why there's no simple compiler invocation or switch; what you want simply doesn't exist.

也就是说,如果您可以使用自定义代码,则只需运行例如

That said, if you can use custom code, you can just run e.g.

#define AAA 1
#define BBB 10
#define CCC (AAA+BBB)
CCC

通过 gcc -P -E ,结果将仅为(1 + 10).

这篇关于确定C宏的扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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