【问题标题】:#define error: expected declaration specifiers or '...' before '(' token#define 错误:预期的声明说明符或 '...' 在 '(' 标记之前
【发布时间】:2014-11-20 00:27:29
【问题描述】:

你知道我为什么会从这段代码中得到这些错误吗?

#define container_of(ptr, type, member) ({\
    const typeof( ((type *)0)->member ) *__mptr = (ptr);\
    (type *)( (void *) ( (char *)__mptr - offsetof(type,member) ) );})

错误:预期的声明说明符或 '...' 在 '(' 标记
错误:'__mptr' 未声明(在此函数中首次使用)

【问题讨论】:

  • 会不会是__mptr 没有在任何地方声明?
  • 您使用的是 Visual Studio(或任何非 GCC 编译器)吗?
  • 为什么需要括号?他们似乎是问题所在。
  • 这篇文章中没有足够的信息。您是在使用宏时遇到错误,还是在defining宏的行中遇到错误?您能否简要摘录一些编译器错误指向的代码行?

标签: c compiler-errors c-preprocessor


【解决方案1】:

感谢您的帮助!

用每边两个下划线的 typeof 替换 'typeof' 解决了这个问题。

【讨论】:

    【解决方案2】:

    “typeof”是 GCC 扩展 https://gcc.gnu.org/onlinedocs/gcc/Typeof.html

    要使用它,需要使用 -std=gnu11 编译器选项指定 GCC 标准。

    【讨论】:

      【解决方案3】:

      也许你可以用这个替换宏:

      #define container_of(ptr, type, member) \ ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))

      我猜你的宏中使用括号的方式是问题的原因。

      【讨论】:

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