Container_of() 未编译

发布于 2024-10-17 01:35:04 字数 601 浏览 6 评论 0原文

List.h 定义了一个名为list_entry 的宏,它是container_of() 函数的包装器。 一个优雅的函数,看起来非常精致:

考虑这段代码:

tmp = list_entry(pos,(struct Order),ord_Queue);

当我使用 gcc 编译它时,不断弹出预期表达式的错误。

我的结构定义为:

struct Order
        {
         double idNum;
         char* entryTime;
         char* eventTime;
         struct list_head ord_Queue;
        };

当 Arg2 和 Arg3 中使用了多余的括号时,container_of 似乎存在问题,并且仅应为 Arg1 提供一个括号礼貌 此处 。我已经尝试过,但它不起作用。

一些帮助将不胜感激。

List.h defines a macro called list_entry which is a wrapper for container_of() function.
An elegant function, which seems very delicate:

Consider this piece of code:

tmp = list_entry(pos,(struct Order),ord_Queue);

When I compile it using gcc, a constant error of expected expression is popping up.

My structure is defined as:

struct Order
        {
         double idNum;
         char* entryTime;
         char* eventTime;
         struct list_head ord_Queue;
        };

It seems there is a problem with container_of when there are superfluous brackets used in Arg2 and Arg3, and there should be one bracket only for Arg1 courtesy here . I have tried it, but it doesnt work.

Some help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以笙箫默 2024-10-24 01:35:04

也许当您从内核复制 list.h 时存在错误? (假设您正在此处执行用户空间程序。)因为您的示例代码(稍微剥离一点)确实使用已知良好的实现进行编译。

#include <libHX/list.h>
struct order {
        struct HXlist_head ord_queue;
};
int main(void) {
        struct HXlist_head *pos;
        struct order *o = HXlist_entry(pos, struct order, ord_queue);
}

Maybe there is an error in list.h when you copied it from the kernel? (Assuming you are doing a userspace program here.) Because your example code (stripped a little more) does compile with a known-good implementation.

#include <libHX/list.h>
struct order {
        struct HXlist_head ord_queue;
};
int main(void) {
        struct HXlist_head *pos;
        struct order *o = HXlist_entry(pos, struct order, ord_queue);
}
ヅ她的身影、若隐若现 2024-10-24 01:35:04

我相信您还缺少包含“offsetof”宏,该宏由container_of()内部使用。尝试包含以下代码(而不是包含整个 list.h )。

#include <sys/types.h>
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({                      \
             const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
             (type *)( (char *)__mptr - offsetof(type,member) );})

#define list_entry(ptr, type, member) \
  container_of(ptr, type, member)

struct list_head {
  struct list_head *next, *prev;
};

并且不需要在第二个参数中添加额外的括号,没有它它应该可以正常工作。
我希望这应该有效。

I believe that you are missing the inclusion of "offsetof" macro as well, which is used by container_of() internally. Try including following code (instead of including whole list.h.

#include <sys/types.h>
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({                      \
             const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
             (type *)( (char *)__mptr - offsetof(type,member) );})

#define list_entry(ptr, type, member) \
  container_of(ptr, type, member)

struct list_head {
  struct list_head *next, *prev;
};

And there is no need of adding extra parenthesis in 2nd argument, it should work fine without it.
I hope this should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文