【问题标题】:"error: expected expression before struct" at macro argument to `offsetof` inside a macro function with musl“错误:结构之前的预期表达式”在带有 musl 的宏函数内的 `offsetof` 的宏参数处
【发布时间】:2020-06-19 13:56:08
【问题描述】:

gcc 使用musl libc 引发以下错误

device_achat.c:192:29: error: expected expression before ‘struct’
  return container_of(_iocb, struct ffs_request, iocb);
                             ^~~~~~
device_achat.c:52:45: note: in definition of macro ‘container_of’
         (type *)( (char *)__mptr - offsetof(type,member) );})
                                             ^~~~

device_achat.c

...
...
#define container_of(ptr, type, member) ({                      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})
...
...
/* Use container_of() to get ffs_request from iocb */
static inline struct ffs_request *to_ffs_request(struct iocb *_iocb)
{
    return container_of(_iocb, struct ffs_request, iocb);
}
...
...

【问题讨论】:

    标签: c gcc macros musl


    【解决方案1】:

    没有看到更多代码,我最好的猜测是程序没有包含stddef.h来获取offsetof,而GCC是(错误的;这真的真的应该很难错误)将其视为隐式声明的函数而不是宏。

    如果这只发生在 musl,而不是 glibc 或您尝试过的其他系统上,则可能是其他 libc 通过从其他标头中隐式包含 stddef.h 来轻微违反命名空间。

    请注意,您可以使用 -Werror=implicit-function-declaration 将隐式函数事物设为错误以捕获此类错误。

    【讨论】:

    • make CFLAGS="-Werror=implicit-function-declaration" 没有在 glibc 上抛出错误
    • @Necktwi:确实,因为某些标头隐式拉入stddef.h,因此存在offsetof 宏定义。添加该选项只会在未定义问题时帮助识别问题(如在 musl 上,因为您没有包含任何可以定义它的内容)。
    • 好吧,#include <stddef.h> 进行了干净的编译!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    相关资源
    最近更新 更多