最简单的模块——hello world

来源:互联网 发布:ptc系列软件 编辑:程序博客网 时间:2024/05/22 06:27

模块代码部分:

/* * ===================================================================================== * *       Filename:  hello.c * *    Description:   * *        Version:  1.0 *        Created:  2011年09月19日 20时18分52秒 *       Revision:  none *       Compiler:  gcc * *         Author:  YOUR NAME (),  *        Company:   * * ===================================================================================== */#include <linux/kernel.h>#include <linux/module.h>static int __init hello_init(void){printk("Hello world!\n");return 0;}static void __exit hello_exit(void){printk("Goodbye cruel world!\n");}module_init(hello_init);module_exit(hello_exit);MODULE_AUTHOR("sunsea");MODULE_DESCRIPTION("module example");MODULE_LICENSE("GPL");

Makefile部分:

ifneq ($(KERNELRELEASE),)obj-m := hello.oelseKERNELSRC :=/lib/modules/`uname -r`/buildmodules:make -C $(KERNELSRC) SUBDIRS=$(PWD) $@clean:rm -f *.o *.ko *.mod.c *~endif

编译步骤:

make

加载模块过程:

  sudo insmod hello.ko

查看运行状态:

dmesg

在最后出现Hello world!

卸载模块过程:

sudo rmmod hello

是否真正卸载:

dmesg

在最后会出现Goodbye cruel world!


原创粉丝点击