简体   繁体   English

Linux内核模块编程的编译错误

[英]Compilation error for Linux kernel module programming

I am learning Linux kernel module programming. 我正在学习Linux内核模块编程。 I am using Beaglebone black for that. 我正在为此使用Beaglebone黑色。 I have made simple 'Hello World' application and makefile. 我已经制作了简单的“ Hello World”应用程序和makefile。 I have checked my makefile it's proper. 我检查了我的makefile文件是否正确。 But when I command 'make', it gives me following error: 但是当我命令“ make”时,它给了我以下错误:

root@beaglebone:/home/sonu# make
make: Warning: File `Makefile' has modification time 2.2e+02 s in the future
make -C /lib/modules/3.8.13-bone70/build M=/home/sonu modules
make: *** /lib/modules/3.8.13-bone70/build: No such file or directory.  Stop.
make: *** [all] Error 2

Though I referred some websites. 虽然我提到了一些网站。 But, all they are asking me to install packages. 但是,他们都要求我安装软件包。 As I am a newbie. 因为我是新手。 I don't even know how to configure and start internet connection on Beaglebone using ethernet. 我什至不知道如何使用以太网在Beaglebone上配置和启动Internet连接。 Please help me I am stuck. 请帮助我,我被卡住了。 Thanks in advance. 提前致谢。

Code is: 代码是:

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

static int __init hello(void)
{
   printk(KERN_INFO "Hello World!");
   return 0;
}

static void __exit hello_cleanup(void)
{
   printk(KERN_INFO "Bye");
}

module_init(hello);
module_exit(hello_cleanup);

Makefile is: Makefile是:

obj-m+=Hello.o

all:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
        $(CC) Hello.c -o test
clean:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
        rm test

It appears that you're trying to build your module for a kernel version that you haven't installed the headers for. 看来您正在尝试为尚未安装标头的内核版本构建模块。

Instead of calling uname directly in your rules, it's helpful to put that into a variable you can override: 与其直接在规则中调用uname将其放入可以覆盖的变量中很有帮助:

obj-m+=Hello.o

KSRC := /lib/modules/$(shell uname -r)/build

all:
        make -C $(KSRC) M=$(PWD) modules
        $(CC) Hello.c -o test
clean:
        make -C $(KSRC) M=$(PWD) clean
        rm test

Now, you can override with the actual location of your kernel headers: 现在,您可以覆盖内核头文件的实际位置:

make KSRC=/usr/src/linux-headers-4.9.2 all

You can simplify the Makefile further with a catch-all rule: 您可以使用包罗万象的规则进一步简化Makefile:

obj-m+=Hello.o

KSRC := /lib/modules/$(shell uname -r)/build

all: modules
        $(CC) Hello.c -o test

%:
        make -C $(KSRC) M=$(PWD) $@

clean::
        make -C $(KSRC) M=$(PWD) clean
        $(RM) test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM