本文共 1533 字,大约阅读时间需要 5 分钟。
在linux-3.4/drivers/目录中执行以下命令:
cd linux-3.4/drivers/mkdir hello
// 所有模块都需要的头文件#include#include #include #define MODULE_LICENSE "GPL"#define MODULE_AUTHOR "baoli"#define MODULE_DESCRIPTION "hello world module"static int __init hello_init(void) { printk(KERN_WARNING "hello world.\n"); return 0;}static void __exit hello_exit(void) { printk(KERN_WARNING "hello exit!\n");}module_init(hello_init);module_exit(hello_exit);
# 如果需要在用户空间编译为模块(m)# 如果需要直接编译成内核模块(y)# 如果不需要编译(n)# 例如:make menuconfig ARCH=arm
menu "HELLO TEST Driver"comment "HELLO TEST Driver Config"config HELLOtristate "hello module test"default mhelp 这是hello测试驱动endmenu
进入linux-3.4/drivers/目录,对Makefile和Kconfig进行修改:
obj-$(CONFIG_HELLO) += hello/
source "drivers/hello/Kconfig"
(注意:某些内核版本可能需要在arch/arm/Kconfig中添加相同内容)
完成以上设置后,进入菜单配置并进行编译:
make menuconfig ARCH=arm
进入“Device Drivers”选项,您可以看到新增的“HELLO TEST Driver”选项。进入该选项后,您可以选择以下编译选项:
LD drivers/hello/built-in.oCC [M] drivers/hello/hello.oCC drivers/hello/hello.mod.oLD [M] drivers/hello/hello.ko
CC drivers/hello/hello.oLD drivers/hello/built-in.o
编译完成后,drivers/hello/目录中会生成hello.o和hello.ko文件。同时,/linux-3.4/output/lib/modules/3.4.39/目录中也会包含生成的hello.ko文件。
希望以上内容能为您的内核开发提供帮助!如果需要进一步优化或者有其他问题,请随时联系!
转载地址:http://mpflz.baihongyu.com/