静态模块编译:
本文为实现在内核的./kernel/drivers/char添加 hello模块
在hello文件夹下,创建以下文件:hello.c、Kconfig、Makefile
hello.c文件:「摘自linux kernel development」
Makefile 文件
在Device drivers下找到Character Device的”Create a hello module“,选勾,保存文件。
本文为实现在内核的./kernel/drivers/char添加 hello模块
wiwa@tech:~/sourcecodes/kernel/drivers/char$ mkdir hello
在hello文件夹下,创建以下文件:hello.c、Kconfig、Makefile
hello.c文件:「摘自linux kernel development」
<span style="font-size:18px;"> /*</span>
* hello.c - The Hello, World! Kernel Module
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
/*
* hello-init - the init function, called when the module is loaded.
* Return zero if successfully loaded, nonzero otherwise.
*/
static int hello_init(void)
{
printk(KERN_ALERT "I bear a charmed life.\n");
return 0;
}
/*
* hello_exit - the exit function, called when the module is removed.
*/
static void hello_exit (void)
{
printk(KERN_ALERT "Out, out, brief candle!\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Shakespeare");
MODULE_DESCRIPTION("A Hello, world Module");
Kconfig文件:
#
# hello configuration
#
config HELLO
tristate 'Create a hello module'
help
We want to create the files only for testing
Makefile 文件
#Makefile for Hello device drivers.
obj-$(CONFIG_HELLO) += hello.o
修改./kernel/drivers/char/Kconfig文件,添加以下代码:
source "drivers/char/hello/Kconfig"
修改./kernel/drivers/char/Makefile文件,添加以下代码:
obj-$(CONFIG_HELLO) += hello/
查看配置效果
wiwa@tech:~/sourcecodes/kernel$ make gconfig
在Device drivers下找到Character Device的”Create a hello module“,选勾,保存文件。
查看配置输出,找到了刚才配置的CONFIG_HELLO参数了
wiwa@tech:~/sourcecodes/kernel$ cat .config | grep HELLO
CONFIG_HELLO=y
编译内核镜像,放在相应启动位置/boot文件夹下,从dmesg找到“hello”被编译进内核启动时的printk信息。