c - linux/初始化.h : No such file or directory

标签 c linux module kernel debian

我正在尝试为我的类(class)构建一个内核模块,但我遇到了一大堆错误,但在这堵墙的顶部是臭名昭著的“没有这样的文件或目录”错误。这似乎是问题的根源。这似乎不仅会影响 init.h,还会影响 module.h 和 kernel.h。程序的前三行如下:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

在浏览类似问题时,我环顾四周并尝试了这些文件应该位于何处的其他路径,但到目前为止没有任何效果。最奇怪的是我已经使用了这个模块;我得到的起始代码在顶部有这个(我没有改变任何东西)并且它没有给我那个错误。虽然,显然后面的代码不一样,但这似乎是目前最大的问题。

完整代码如下:

#include </usr/include/linux/init.h>
#include </usr/include/linux/module.h>
#include </usr/include/linux/kernel.h>

/* This function is called when the module is loaded. */
int simple_init(void)
{
    printk(KERN_INFO "Loading Module\n");
    static LIST_HEAD(birthday_list)
    struct birthday{
        int day;
        int month;
        int year;
        struct list_head list;
    };
    struct birthday *ptr, *next;
    struct birthday *bob;
    struct birthday *judy;
    struct birthday *josh;
    struct birthday *lana;
    struct birthday *jan;

    bob = kmalloc(sizeof(*bob), GFP_KERNEL);
    bob -> day = 17;
    bob -> month = 1;
    bob -> year = 1990;
    INIT_LIST_HEAD(&bob -> list);

    ...

    list_add_tail(bob -> list, &birthday_list);
    list_add_tail(judy -> list, &birthday_list);
    list_add_tail(josh -> list, &birthday_list);
    list_add_tail(lana -> list, &birthday_list);
    list_add_tail(jan -> list, &birthday_list);

    struct birthday *ptr;

    list_for_each_entry(ptr, &birthday_list, list){

        kprintf('%d/%d/%d \n', ptr -> month, ptr -> day,  ptr -> year);
    }

    list_for_each_entry_safe(ptr, &birthday_list, list){

        list_del(&ptr->list);
        kfree(ptr);
    }

       return 0;
}

/* This function is called when the module is removed. */
void simple_exit(void) {
    printk(KERN_INFO "Removing Module\n");
}

/* Macros for registering module entry and exit points. */
module_init( simple_init );
module_exit( simple_exit );

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
MODULE_AUTHOR("SGG");

最佳答案

我认为您必须首先通过 apt-get 安装类似 linux-headers-[内核版本] 的东西,然后您必须按如下方式创建 Makefile:

ifneq ($(KERNELRELEASE),)
    # call from kernel build system
    lifo-objs := main.o
    obj-m   := lifo.o
else
   KERNELDIR ?= /lib/modules/$(shell uname -r)/build
   PWD       := $(shell pwd)
modules:
    echo $(MAKE) -C $(KERNELDIR) M=$(PWD) LDDINC=$(PWD)/../include modules
    $(MAKE) -C $(KERNELDIR) M=$(PWD) LDDINC=$(PWD)/../include modules
endif

clean:  
    rm -rf *.o *~ core .depend *.mod.o .*.cmd *.ko *.mod.c \
    .tmp_versions *.markers *.symvers modules.order

depend .depend dep:
    $(CC) $(CFLAGS) -M *.c > .depend

ifeq (.depend,$(wildcard .depend))
    include .depend
endif

在上面的 Makefile 中将 KERNELDIR 变量设置为您合适的内核版本,默认情况下它使用您正在运行的内核。如果您使用此 Makefile,则需要将您的包含更改为以下格式:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

我认为内核模块开发使用来自 Linus Torvalds git 的标准内核更好。对于一些简单的内核模块,请参阅 this .

关于c - linux/初始化.h : No such file or directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28423477/

相关文章:

c - 将 uint 写入/读取到二进制文件

linux - QProcess 传递(shell)参数

linux - 使用 xorg 在 Linux 下的 FreeGLUT 中共享上下文

python - 使用 random 模块生成一维随机游走

javascript - 使用 typescript 的匿名模块

c - 数据包复制的影响

c - 如何连续在子进程和父进程之间进行上下文切换?

java - 安装Maven 3.0.4

ruby - 如何在动态生成的模块中运行 Proc?

python - ffmpeg 在 python 中使用 libardrone