Generally, you can use a general makefile when writing the kernel driver, saving you from the trouble of writing your own. The following is a file I used: ifneq ($ (kernelrelease ),)
# Call from kernel build system
Module-objs: = Hello. o
OBJ-M: = Hello. o
Else
Kerneldir? =/Lib/modules/$ (shell uname-R)/build
PWD: = $ (shell PWD)
Modules:
$ (Make)-C $ (kerneldir) M = $ (PWD) Modules
Endif
Clean:
Rm-RF *. O *~ Core. depend. *. CMD *. Ko *. Mod. C. tmp_versions
Module-objs: = This is followed by a general. o file collection. How many. c files are there? How many. O files are there? For example:
File1.c and file2.c
The statement is as follows:
Module-objs: = file1.o file2.o
Finally, the module in Module-objs should be replaced by the name of the. o file in obj-M.
Hello-objs: = Hello. o
OBJ-M: = Hello. o
If there are multiple files in Module-objs: = file1.o file2.o, do not duplicate the. o file in obj-M: = Hello. O with the name in Module-objs.
Let's take a look at two examples. file C, hello. C and hello1.c are as follows: /* ===================================================== ======================================
A simple kernel module: "Hello World"
========================================================== ===================================== */
/*
* Hello. c
*/
# Include <Linux/init. h>
# Include <Linux/module. h>
Module_license ("GPL ");
Static int hello_init (void)
{
Printk (kern_alert "Hello world in, ztz0223 ");
Return 0;
}
Module_init (hello_init );
Module_author ("ztz0223 ");
Module_description ("a simple hello World module ");
Module_alias ("a simplest module ");
// Hello1.c
# Include <Linux/init. h>
# Include <Linux/module. h>
Static void hello_exit (void)
{
Printk (kern_alert "Hello World quit, ztz0223! ");
}
Module_exit (hello_exit );
Makefile:
Ifneq ($ (kernelrelease ),)
# Call from kernel build system
My_hello-objs: = Hello. O hello1.o
OBJ-M: = my_hello.o
Else
Kerneldir? =/Lib/modules/$ (shell uname-R)/build
PWD: = $ (shell PWD)
Modules:
$ (Make)-C $ (kerneldir) M = $ (PWD) Modules
Endif
Clean:
Rm-RF *. O *~ Core. depend. *. CMD *. Ko *. Mod. C. tmp_versions
The final kernel module is my_hello.ko, which can be loaded and uninstalled.
Another point is that the makefile above should not be copied directly from above, but should be manually written as above.