error: field ‘nm_creds’ has incomplete type

error: field ‘nm_creds’ has incomplete type

下列结构的正确顺序是什么?它的投掷字段有一个不完整的类型错误。

#include <stdlib.h>
struct nl_msg
{   
    int         nm_protocol;
    int         nm_flags;
    struct ucred        nm_creds;
    struct nlmsghdr *   nm_nlh;
    size_t          nm_size;
    int         nm_refcnt;
};

struct nl_msg;
struct nl_tree;
struct ucred;

int main()
{
    return 0;
}

您的结构包含一个 struct ucred 类型的字段,该字段尚未在任何地方定义。

您需要提供该类型的定义。

在您的代码中(而是翻译单元。),没有任何地方定义 struct ucred

您需要定义 _GNU_SOURCE MACRO 并包含定义此结构的 sys/socket.h header。

See this online.

你的结构中的一个字段nl_msg:

struct ucred        nm_creds;

具有未定义的数据类型;您尚未在任何地方定义 ucred 的结构。您需要在代码中的某处定义结构 ucred 。这就是您收到错误的原因:

field has an incomplete type error.

如果您指的是 socket.h 文件,那么您需要将头文件添加到定义此结构的代码中:

#include <sys/socket.h>

编辑:您还需要定义 _GNU_SOURCE 宏。