首页 文章

构建linux内核模块

提问于
浏览
1

我是一个Windows驱动程序员,是Linux内核开发的新手 . 我已经安装了linux内核头文件 . 我在linux内核中尝试我的helloworld模块 .

#include <linux/init.h>
#include <linux/module.h>
/*MODULE_LICENSE("Dual BSD/GPL");*/
static int hello_init(void)
{
    printk(KERN_ALERT "Hello, world\n");
    return 0;
}
static void hello_exit(void)
{
    printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);

以下是我的模块的代码 . 我的构建的makefile是

obj-m +=tryout.o

KDIR =/usr/src/linux-headers-4.13.0-37-generic

all:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
    rm -rf *.o *.ko *.mod.* *.symvers *.order

但我得到'致命的错误:linux / init.h:制作这个模块时没有这样的文件或目录' . 可能的原因是什么?我该如何解决?

1 回答

  • 0

    您的Makefile配置错误 . 特别是你使用了 SUBDIRS ,而你应该使用 M 而你的$(PWD)没有意义,你应该使用 pwd 来简单(或者是$ PWD);这是你应该如何设置它:

    ifneq ($(KERNELRELEASE),)
        # kbuild part of makefile
        obj-m  := tryout.o
        # any other c files that you would like to include go into 
        # yourmodule-y := <here> e.g.:
    
        # tryout-y := tryout-1.o tryout-2.o 
    
        else
        # normal makefile
        KDIR ?= /usr/src/linux-headers-4.13.0-37-generic
    
        # you really should set KDIR up as:
        # KDIR := /lib/modules/`uname -r`/build
    
        all::
            $(MAKE) -C $(KDIR) M=`pwd` $@
    
        # Any module specific targets go under here
        # 
    
        endif
    

    像这样配置你的makefile只需要在你的模块目录中键入 make ,它就会调用内核的kbuild子系统,后者又会使用你的Makefile的kbuild部分 .

    阅读https://www.kernel.org/doc/Documentation/kbuild/modules.txt,了解有关如何执行此操作的所有不同排列 . 它附带了一些例子 .

相关问题