C - "error: expected expression before struct" 在宏参数中与 musl 的宏函数内部的"偏移"



gcc使用musllibc 抛出以下错误

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) );})
^~~~

设备聊天

...
...
#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);
}
...
...

在没有看到更多代码的情况下,我的最佳猜测是程序没有包含stddef.h来获得offsetof,而GCC(错误地;这真的是真的应该是一个硬错误(将其视为隐式声明的函数,而不是宏。

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

请注意,您可以使用-Werror=implicit-function-declaration将隐式函数thing设置为错误,以捕获这样的错误。

最新更新