参考网上例程
这是hello.c:
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>static int hello_init(void)
{
printk(KERN_INFO "Hello world enter!\n");
}static void hello_exit(void)
{
printk(KERN_INFO "Hello world exit!\n");
}module_init(hello_init)
module_exit(hello_exit)MODULE_AUTHOR("hexing");
MODULE_LICENSE("dual BSD/GPL");
MODULE_DESCRIPTION("SIMPLE");
MODULE_ALIAS("simple");
这是Makefile:
KVERS =$(shell uname -r)
obj-m += hello.o
build: kernel_modules
kernel_modules:
make -C /lib/modules/$(KVERS)/build M=$(CURDIR) modules
clean:
make -C /lib/modules/$(KVERS)/build M=$(CURDIR) clean
直接 make 即可
最近编辑记录 firstman (2019-06-14 22:42:37)
离线
firstman@ubuntu:/opt/test$ make
make -C /lib/modules/4.10.0-38-generic/build M=/opt/test modules
make[1]: Entering directory '/usr/src/linux-headers-4.10.0-38-generic'
CC [M] /opt/test/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /opt/test/hello.mod.o
LD [M] /opt/test/hello.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.10.0-38-generic'
firstman@ubuntu:/opt/test$ sudo insmod hello.ko
firstman@ubuntu:/opt/test$
firstman@ubuntu:/opt/test$ sudo lsmod |grep hello
hello 16384 0
离线