【问题标题】:C MACRO expansion dynamicallyC MACRO 动态扩展
【发布时间】:2022-02-02 23:56:52
【问题描述】:

我正在尝试类似于遵循旧代码的方法

//为PLATFORM 1定义宏

#define PLATFORM_1_ENTRY_L2PT    10
#define PLATFORM_1_ENTRY_L3PT    11
#define PLATFORM_1_ENTRY_L4PT    12
#define PLATFORM_1_ENTRY_L5PT    13

//为PLATFORM 2定义宏

#define PLATFORM_2_ENTRY_L2PT    20
#define PLATFORM_2_ENTRY_L3PT    21
#define PLATFORM_2_ENTRY_L4PT    22
#define PLATFORM_2_ENTRY_L5PT    23

//为默认平台定义宏

#define DEFAULT_PLATFORM_ENTRY_L2PT    30
#define DEFAULT_PLATFORM_ENTRY_L3PT    31
#define DEFAULT_PLATFORM_ENTRY_L4PT    32
#define DEFAULT_PLATFORM_ENTRY_L5PT    33

//获取基于“动态”平台类型的偏移量

#define GET_OFFSET(entry_type)   ({                                          \
     (global.platform_1) ?                                                   \
         PLATFORM_1_ENTRY_##entry_type :                                     \
         (global.platform_2) ?                                               \
              PLATFORM_2_ENTRY_##entry_type :                                \
              DEFAULT_PLATFORM_ENTRY_##entry_type)                           \
     })  

//使用下面的宏扩展根据平台类型进行扩展

#define ENTRY_L2PT                                       GET_OFFSET(L2PT)
#define ENTRY_L3PT                                       GET_OFFSET(L3PT)
#define ENTRY_L4PT                                       GET_OFFSET(L4PT)
#define ENTRY_L5PT                                       GET_OFFSET(L5PT)

//定义全局结构

struct global_ {
    bool platform_1;
    bool platform_2;
} glbl;

glbl global = {0};

//从示例主目录调用


void main() {
    //set offset 
    int array[40] = {0};
    ...
    if(sizeof (void *) * CHARBIT == 64) {
        global.platform_1 = 1;
    } else {
        global.platform_2 = 1;
    }
    array[ENTRY_L2PT]++;
    array[ENTRY_L3PT]++;
    array[ENTRY_L4PT]++;
    array[ENTRY_L5PT]++;
    ...
}

** 我在编译期间收到以下错误:**

At top level:
error: braced-group within expression allowed only inside a function
#define GET_OFFSET(entry_type)   ({                                        \
                                  ^
note: in expansion of macro ‘GET_OFFSET’
#define ENTRY_L2PT                GET_OFFSET(L2PT)
                                             ^
note: in expansion of macro ‘ENTRY_L2PT’
        array[ENTRY_L2PT]++;

我该如何进行上述工作?

请注意,在代码中使用 ENTRY_L2PT 的地方有很多,并且在开发/测试/回归等方面都需要付出很多努力。

【问题讨论】:

  • 在修复了几个拼写错误并添加了缺失的标题后,我得到了一个完全不同的错误 (expected ';' before ')' token),指向最后一个拼写错误:##entry_type),应将其替换为 ##entry_type;。如果这没有帮助,请提供minimal reproducible example,我们可以编译并看到与您相同的错误。
  • 除非所有这些宏始终存在,否则您不能使用这样的解决方案,因为所有PLATFORM_1_ENTRY_##entry_typePLATFORM_2_ENTRY_##entry_type 等总是会被扩展。他们总是在场吗?
  • 是的,PLATFORM_1_ENTRY_##entry_type、PLATFORM_2_ENTRY_##entry_type 等始终存在。
  • 您确定这是您使用的代码吗?你不会得到 CHARBIT 是 undefiend,struct 缺少 typedef,你不会得到 ``expected ';'在 ')' token` 错误之前?您确定您使用的是您发布的代码吗?

标签: c macros preprocessor


【解决方案1】:

我该如何进行上述工作?

表达式内的括号组只允许在函数内

不要使用大括号。

#define GET_OFFSET(entry_type)   ( \
     .... \
     .... \
     )

【讨论】:

    猜你喜欢
    • 2013-03-31
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 2018-03-12
    相关资源
    最近更新 更多