【问题标题】:error: expected identifier or '(' before ')' token after changing a macro [closed]错误:更改宏后的预期标识符或“(”之前的“)”标记[关闭]
【发布时间】:2022-01-17 13:25:02
【问题描述】:

在 C 程序(u-boot)中,为了检查程序是否运行该宏,我更改了定义状态
来自

#define debug_cond(cond, fmt, args...)                  \
({                                  \
    if (cond)                           \
        log(LOG_CATEGORY,                   \
            (enum log_level_t)(LOGL_FORCE_DEBUG | _LOG_DEBUG),  \
            fmt, ##args);                   \
})

#define debug_cond(cond, fmt, args...)                  \
({                                  \
extern uint64_t myptr;              \
    if (cond) {                         \
        *((uint64_t *)myptr) = 0x801; myptr+=8; \ 
        log(LOG_CATEGORY,                   \
            (enum log_level_t)(LOGL_FORCE_DEBUG | _LOG_DEBUG),  \
            fmt, ##args);                   \
    }
})                 <==== line 267

编译器给了我这个错误信息。

In file included from include/linux/printk.h:4,
                 from include/common.h:20,
                 from lib/asm-offsets.c:14:
include/log.h:267:1: error: expected identifier or '(' before '}' token
  267 | })
      | ^
include/log.h:267:2: error: expected identifier or '(' before ')' token
  267 | })

我不知道这里出了什么问题。谁能告诉我哪里做错了?

【问题讨论】:

  • 您是否检查过宏中的所有行都有 line-continuation 转义?
  • 啊,是的,第 266 行最后没有 line continuation。写一个简单的答案,以便我可以选择它。

标签: c compiler-errors c-preprocessor


【解决方案1】:

在第 5 个宏行的结尾 \ 后面有一个空格。删除尾随空格:

#define debug_cond(cond, fmt, args...)                  \
({                                  \
extern uint64_t myptr;              \
    if (cond) {                         \
        *((uint64_t *)myptr) = 0x801; myptr+=8; \
        log(LOG_CATEGORY,                   \
            (enum log_level_t)(LOGL_FORCE_DEBUG | _LOG_DEBUG),  \
            fmt, ##args);                   \
    }
})

空格将宏定义分成两半,而后半部分正在被编译器解释,导致语法错误。

从长远来看,打开编辑器的“显示空白字符”选项可能会很有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多