Linux was just beginning to support Intel 386, and was later migrated to more and more platforms, including ARM, POWERPC, all of the code device driver code are compiled into the kernel, this is obviously not realistic, it is necessary to use the form of kernel modules to load the driver. Of course the module is not necessarily a driver, but it can also provide a function for the driver.
Now, write a simple Linux module first.
1#include <linux/init.h>2#include <linux/module.h>3 4Module_license ("GPL");5Module_author ("Bunfly");6 7 intTest_init ()8 {9Printk"This is kernel init\n");Ten One return 0; A } - - voidtest_exit () the { -Printk"Bye bye\n"); - - } + - Module_init (test_init); +Module_exit (Test_exit);
modules are dynamically loaded into the kernel and are part of the kernel, so there is no main function , and the 20th line of the module initialization function is a callback function that calls the Test_init function when the Load Module command Insmod executes. The same is the 21-row module unload function.
Each module function consists of init.h module.h two header files.
When writing a module function, declaring the module's authorization protocol, if not, the compiler has a warning, if the code of the device-driven model that is called in the module function, it must be specified as the GPL protocol, otherwise it cannot be loaded into the kernel.
Using PRINTK instead of printf in Test_init (), the difference is that PRINTK is used by the kernel and does not support floating-point operations. The PRINTK can specify the priority of the output.
The modules that are written are compiled using the makefile in the kernel source code. The following makefile is the specified kernel source makefile path, as well as the module generation path.
1 All : 2 Make-c/home/bunfly/bunfly/source_code/linux-3.5 m=' pwd '34clean: 5 Make-c/home/bunfly/bunfly/source_code/linux-3.5 m=' pwd ' clean67 Obj-m + = TEST.O
There are three types of compilation:
obj-not programmed into the kernel
Obj-y programmed into the kernel
Obj-m compiled into modules
in the source code is obj-(macro), in fact, the compilation kernel of make menuconfig or make Config is the value of the configuration macro.
This program is obviously compiled into a module using OBJ-M, the latter test.o that the compiler will automatically go to find test.c or test.s files.
Place test.c and makefile in the same directory, execute make, and the Test.ko file is generated.
Move the Test.ko to the 4412 board, execute the insmod test.ko load module,
Executing the RMMOD test unload module
This means that the first module program was written successfully.
Day 40th: Compiling loadable Modules