Use the kernel of the host machine to compile the hello world module.
1. Select a working directory. I select/opt/guoqian/hellomod;
Mkdir-p/opt/guoqian/hellomod
2. Use vim to edit hello. c. The Code is as follows:
#include <linux/init.h>#include <linux/module.h>MODULE_LICENSE("Dual BSD/GPL");static int hello_init(void){ printk(KERN_ALERT "Hello,my drive world!\n"); return 0;}static void hello_exit(void){ printk(KERN_ALERT "Goodbye,drive world!\n");}module_init(hello_init);module_exit(hello_exit);
3. Edit Makefile as follows:
Obj-m: = hello. oKDIR: =/lib/modules/2.6.35-22-generic/build // modules: make-C $ (KDIR) M = $ (PWD) modulesclean: rm-f *. ko *. o ,*. mod. o *. mod. c. symvers
4. Compile: make; the following directory appears:
Root @ wyz-vlinux:/opt/guoqian/hellomod # ls
Hello. c hello. mod. c hello. o modules. order
Hello. ko hello. mod. o Makefile Module. symvers
5. Load the module, insmod hello. ko
Hello, my drive world
6. Uninstall the module, rmmod hello. ko
Goodbye, drive world