Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
确定C宏的扩展_C_Gcc_C Preprocessor_Preprocessor Directive - Fatal编程技术网

确定C宏的扩展

确定C宏的扩展,c,gcc,c-preprocessor,preprocessor-directive,C,Gcc,C Preprocessor,Preprocessor Directive,有没有办法通过检查编译后的对象或在.C或.h文件上运行某种形式的gcc-E来确定扩展C宏的“最终值” 测试h #define AAA 1 #define BBB 10 #define CCC (AAA+BBB) 测试c #include <stdio.h> #include "test.h" int main(){ printf("%d\n", CCC); return 0; } 或 如果使用 gcc -dM -E test.c | grep CCC 或 输出

有没有办法通过检查编译后的对象或在.C或.h文件上运行某种形式的gcc-E来确定扩展C宏的“最终值”

测试h

#define AAA 1
#define BBB 10
#define CCC (AAA+BBB)
测试c

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

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

如果使用

gcc -dM -E test.c | grep CCC 

输出

#define CCC (AAA+BBB)
这要求我仍然知道什么是
AAA
BBB

汇编时使用:

gcc -E test.c
给出(跳过样板):

当它扩展到
CCC
时,我现在失去了映射回
CCC

编辑: 由于还不清楚,我希望通过某种方式确定CCC是什么(11或1+10(正如gcc-E示例所示,它只插入了(1+10)而不是11),最好不要修改代码本身。在MRE中使用Printf是个坏主意,我实际上想到的是这样的代码:

struct my_struct {
    int my_array[CCC]
    ... other stuff ...
}
问题是我的_数组有多大,所以我可以用另一种语言(python通过ctypes)创建一个结构,并知道我需要多少空间。我知道对于结构,我可以使用pahole,但希望只使用gcc,在更一般的情况下(比如说,不在结构中的全局数组)

打印相同内容的替代版本:

#include <stdio.h>

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

#define STRINGIFY(x) #x
#define INTERMEDIATE(x) #x " = '" STRINGIFY(x) "'"

int main(void)
{
  printf("%s\n", INTERMEDIATE(CCC));
}
#包括
#定义AAA 1
#定义BBB2
#定义CCC(AAA+BBB)
#定义STRINGIFY(x)#x
#定义中间(x)#x“=”“字符串化(x)””
内部主(空)
{
printf(“%s\n”,中间(CCC));
}

预处理器在任何时候都不会创建

#define CCC (1+10)
CCC
的扩展总是
(AAA+BBB)
;只是宏扩展的结果被重新扫描,以便更多的宏进行扩展,此时
AAA
BBB
分别变成
1
10

也许更清楚的例子是

#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”

此示例还演示了宏可以重新定义,因此对于“CCC的值是多少?”甚至可能没有一个答案

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

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

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

通过
gcc-p-E
,结果将是
(1+10)

我已经读了两遍这个问题。我不明白你在问什么。最后的值不是(1+10)吗?你想要什么输出?类似于
printf(“%s”%d\n),#CCC,CCC)
确定报告宏名称和扩展吗?宏处理是一个全有或全无的过程。代码可以使用
\u Static\u assert()
在编译时测试值,以满足更高级别的遵从性目标
\u Static\u assert(CCC>0和&CCC<20,“CCC超出预期范围”);
#include <stdio.h>

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

#define STRINGIFY(x) #x
#define PASTE(x) STRINGIFY(x)


int main(void)
{
  printf("CCC = '%s'\n", PASTE(CCC));
}
CCC = '(1+10)'
#include <stdio.h>

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

#define STRINGIFY(x) #x
#define INTERMEDIATE(x) #x " = '" STRINGIFY(x) "'"

int main(void)
{
  printf("%s\n", INTERMEDIATE(CCC));
}
#define CCC (1+10)
#define AAA 1
#define CCC AAA
#define AAA "hello"

size_t x = sizeof CCC;
#define AAA 1
#define BBB 10
#define CCC (AAA+BBB)
CCC