获取错误未知类型名称 'size_t' 即使我已经包含了相关的 C 库和头文件

Getting error unknown type name 'size_t' even though I've included the relevant C libraries and headers

请在下面找到我的代码。 我收到错误“/usr/include/linux/sysctl.h:40:2: 错误:未知类型名称‘size_t’”

在线搜索,唯一的建议是确保您的代码中包含 stddef.h,我这样做如下所示。除了我已经尝试过的这个修复之外似乎没有可用的解决方案,所以我目前对如何前进感到茫然。

另请注意,这段代码并不漂亮,但这不是该线程的主要问题。我得到的错误看起来不像是我的代码中的错误引发的,但我可能是错的。

#include <linux/netfilter_ipv4.h>
#include <linux/netfilter.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <sys/types.h>
#include <linux/module.h>
#include <stddef.h>

struct nf_hook_ops{

        struct list_head *list;

        nf_hookfn *hook;

        struct module *owner;

        u_int8_t pf;

        unsigned int hooknum;

        int priority;    /* Hooks are ordered in ascending priority. */

};

int nf_register_hook(struct nf_hook_ops *reg);

void nf_unregister_hook(struct nf_hook_ops *reg);


struct nf_hook_ops nfho = {

    nfho.hook = hook_func_in,     

    nfho.hooknum = NF_INET_LOCAL_IN,     

    nfho.pf = PF_INE, 

    nfho.priority = NF_IP_PRI_FIRST
};

nf_register_hook(&nfho);         // Register the hook

C 严格从上到下解析,#include 包含普通的旧文本,而不是任何符合 "module import" 名称的巧妙内容。因此,#include 指令的顺序很重要。在这种情况下,您会收到关于由 stddef.h 定义的类型的投诉,因此您必须确保 之前 包含任何需要的类型,这可能是(实际上是)另一个头文件。

我可以使用以下两行源文件重现您遇到的错误:

#include <linux/sysctl.h>
#include <stddef.h>

$ gcc -fsyntax-only test.c
In file included from test.c:1:0:
/usr/include/linux/sysctl.h:39:2: error: unknown type name ‘size_t’

如果我交换 #include 行的顺序,

#include <stddef.h>
#include <linux/sysctl.h>

那就没有错误了。这是 linux/sysctl.h 中的一个错误,但我不会屏住呼吸等待它被修复。我建议将 stddef.h 移动到包含列表的最顶部。

我可以不能用你的实际包含列表重现问题,

#include <linux/netfilter_ipv4.h>
#include <linux/netfilter.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <sys/types.h>
#include <linux/module.h>
#include <stddef.h>

但是 gcc -H 转储不显示 linux/sysctl.h 被那组包含传递地拉入,所以可能只是我的 [=42] 上有不同版本的内核头文件=]盒子比你做的。