首先新建一个目录(比如):
mkdir helloworld
然后进入到这个目录:
cd helloworld
第一步:
vi hello.c
#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);
第二步:
vi Makefile
obj-m := hello.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
第三步:
基本的程序写好后,运行时遇到的一些问题和解决方案:
[lijun@myhost helloworld]$ /lib/modules/$(shell uname -r)/build
bash: shell: command not found
bash: /lib/modules//build: 没有那个文件或目录
[lijun@myhost helloworld]$ echo /lib/modules/$(uname -r)/build
/lib/modules/2.6.35-ARCH/build
[lijun@myhost helloworld]$ ls /lib/modules/$(uname -r)/build
ls: 无法访问/lib/modules/2.6.35-ARCH/build: 没有那个文件或目录
[lijun@myhost helloworld]$ pacman -Ss header
core/kernel26-headers 2.6.35.8-1
Header files and scripts for building modules for kernel26
core/kernel26-lts-headers 2.6.32.25-2
本文介绍了一个简单的Linux模块编写过程,包括创建目录、编写模块代码、设置Makefile等步骤,并解决了在构建过程中可能遇到的问题。
2779

被折叠的 条评论
为什么被折叠?



