Http://hi.baidu.com/ah%5F%5Ffu/blog/item/57b76201e27095de267fb5d4.html Finally, I made up my mind to study the Linux kernel and drive development. I was not afraid of the shame of tens of millions of people. I was brave enough to write my learning process: 1. About Directories /Lib/modules/2.6.9-42. elsmp/build/the directory where the kernel source code is located Generally, run the following command to enter the Directory: CD/lib/modules/$ (uname-R)/build/ This directory actually points to:/usr/src/kernels/2.6.9-42. EL-smp-i686 2. Compile the makefile used by the driver. In fact, when compiling the driver, a preset makefile is used, which is located: /Lib/modules/$ (uname-R)/build/makefile Note: m is capitalized. 3. source code of the Linux driver Hello World copied online: // Hello. c # Include <Linux/init. h> # Include <Linux/module. h> Module_license ("dual BSD/GPL "); Static int hello_init (void) { Printk (kern_alert "Hello world! \ N "); Return 0; } Static void hello_exit (void) { Printk (kern_alert "goodbye! \ N "); } Module_init (hello_init ); Module_exit (hello_exit ); 4. Write a makefile to compile the driver: (version 1 is the simplest) # The following line shows the content of the makefile. Note that M is capitalized. OBJ-M: = Hello. o Save hello. C and makefile in the same directory, and then execute: Make-C/lib/modules/'uname-R'/build subdirs = $ PWD modules In this way, the driver is compiled, and the result is the hello. Ko file. Note: makefile must be written as makefile. If it is written as makefile, it cannot be compiled. (It takes more than n time to complete this step) 5. Write another makefile: (version 2: the most time-consuming) # The content of makefile is as follows: OBJ-M: = Hello. o Kernel_dir: =/lib/modules/$ (shell uname-R)/build PWD: = $ (shell PWD) ALL: Make-C $ (kernel_dir) subdirs = $ (PWD) Modules Clean: RM *. O *. Ko Then execute: make the compilation is successful, and the command line does not add parameters, which is very easy. Note: All: And clean: The following row. The front is a tab key. 6. Load the driver: Run Insmod./Hello. Ko The screen does not respond. (Because I used a remote terminal to connect to Windows) OK. Let's let the time go back and go back to loading the driver. open another window and execute: Tail-F/var/log/message Then execute the following in the original window: Insmod./Hello. Ko Haha, the long-awaited Hello world is displayed in the/var/log/message file! 7. view the driver: Lsmod saw the hello driver in it. 8. Uninstall the driver: Rmmod hello Goodbye is displayed in/var/log/message. |