1.先来一段测试代码。
#include//linux 头文件 #include //linux编译模块需要的头文件 static int hello_init(void) //入口函数,int类型有返回值 { printk(KERN_ERR "helloworld!"); return 0; } static void hello_exit(void) //出口函数,void类型没有返回值 { printk(KERN_ERR "byebye!!"); } module_init(hello_init); //模块入口 module_exit(hello_exit); //模块出口 MODULE_LICENSE("GPL"); //声明符合GPL标准
2.编译的makefile
obj-m += helloworld.o
KERN_DIR:=/home/alon/linuxstudy/linux-core/linux-4.4.154
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
执行make后可以看到有helloworld.ko文件
3.在板子上加载/卸载驱动模块
看到了printk输出的"helloworld!"和"byebye!!",代码里面没加换行,呵呵呵呵呵,这样就OK了,查看用lsmod,对,就这样。



