【问题标题】:error: expected declaration specifiers or '...' before '(' token错误:预期的声明说明符或 \'(\' 标记之前的 \'...\'
【发布时间】:2023-01-19 23:00:00
【问题描述】:

我尝试在 C 中使用 #define 预处理器指令,如下所示:

/* This part is okay */

#define TEST1  (uint8)0x00

#define TEST2  (uint8)0x20


/* Here is where the problem occurs */

#define L2_PORT_USB_SET    ( TEST1 )   /* comment comes here       */

#define L2_SUBS_WB  ( TEST1 | (uint8)0x01 )          /* another comment comes here                      */

我做错了什么?

【问题讨论】:

  • 我不认为这个错误会在定义宏时发生,它会在使用宏时发生。

标签: c c-preprocessor


【解决方案1】:

使用 #define 创建的宏扩展到行尾,因此您拥有的 cmets 是宏的一部分。

这意味着这样的代码:

int x = L2_PORT_USB_SET + 1;

将扩展为无效的:

int x = (uint8)0x00    /* comment comes here       */ + 1;

您需要将 cmets 放在单独的行中:

/* comment comes here       */
#define L2_PORT_USB_SET    ( TEST1 )

/* another comment comes here                      */
#define L2_SUBS_WB  ( TEST1 | (uint8)0x01 )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 2011-09-04
    相关资源
    最近更新 更多