编写一个简单的内核驱动模块时报错 “/lib/modules/3.13.0-32-generic/bulid: 没有那个文件或目录。 停止。”

编写一个简单的内核驱动模块

 1 static int hello_init()
 2 {
 3     printk(“hello,I am in kernel now
”);
 4     return 0;
 5 }
 6 void addfunc(int a,int b)
 7 {return a+b;}
 8 static void hello_exit()
 9 {
10     printk(“hello ,I will leave the kernel now
”);
11 }
12 module_init(hello_init);
13 module_exit(hello_exit);
14 MODULE_LICENSE(“GPL”);

Makefile文件:

1 obj-m := hello.o
2 KDIR := /lib/modules/$(shell uname –r)/build
3 PWD := $(shell pwd)
4 all:
5     $(MAKE) –C $(KDIR) SUBDIRS=$(PWD) modules 

报错 “/lib/modules/3.13.0-32-generic/bulid: 没有那个文件或目录。 停止。”

网上车了一下说是没安装内核安装包(类似于kernel-devel的名字)或者是链接出错,但问题是,我的内核安装包有,链接也没问题,可他就是报错。

后来对比了我之前的Makefile文件,然后改动一下:

1 obj-m:=hello.o
2 KDIR:=/lib/modules/`uname -r`/build
3 PWD:=$(`pwd`)
4 all:
5         $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

然后就可以了。。。。。。。。。。

编译结果如下:

又报错"没有规则可以创建“arch/x86/syscalls/../include/generated/uapi/asm/unistd_32.h”需要的目标“/usr/src/linux-headers-3.13.0-32-generic/arch/x86/syscalls/syscall_32.tbl”。 停止。"

好吧,其实还是Makefile的锅,注意看第三行 PWD:=$(`pwd`) ,应该改成 PWD:=`pwd` 

 再次编译,没错,还是报错,但这次不是Makefile的锅啦~

报错信息"错误: 函数声明不是一个原型 [-Werror=strict-prototypes]"

简单,你在那些无参函数的参数列表里填上"void "就好啦~

最后老师在 MODULE_LICENSE("GPL"); 那里报错: /home/branches/chenyue/modules/hello.c:17:16: 错误: expected declaration specifiers or ‘...’ before string constant ,网上说可能是因为代码里面含有中文格式,可我在VIM下重新码了一遍那个错误还是不消失啊,所以干脆把这行开放源码许可给删了,编译通过~~~~

 所以,最后的源文件跟Makefile:

 1 #include <linux/kernel.h>
 2 static int hello_init(void )
 3 {
 4         printk("hello,I am in kernel now
");
 5         return 0;
 6 }
 7 
 8 int addfunc(int a, int b)
 9 {       return (a+b); }
10 
11 static void hello_exit(void )
12 {
13         printk("hello, I will leave the kernal now
");
14 }
15 module_init(hello_init);
16 module_exit(hello_exit);
1 obj-m:=hello.o
2 KDIR:=/lib/modules/`uname -r`/build
3 PWD:=`pwd`
4 all:
5         $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
原文地址:https://www.cnblogs.com/fallenmoon/p/6910681.html