Write the makefile file directly (assuming the file to be compiled is HELLO.C) 1 Ifneq ($ (kernelrelease),) 2 Obj-m: = hello.o 3 Else 4 Obj-m: = hello.o 5 Kerneldir =/lib/modules/$ (Shell uname-r)/build 6 pwd: = $ (Shell PWD) 7 8 Default: 9 $ (make)-C $ (Kerneldir) m=$ (PWD) modules Ten endif The first ifeq ($ (kernelrelease),) is currently useless, and its origin is that the Kernelrelease macros are defined when the kernel is makefile compiled under the Linux source root directory, Then if you start from the source root, make will compile the MYHELLO.O module into the kernel. Kerneldir =/usr/src/$ (Shell uname-r), this sentence is assigned to the KERNELDIR, this variable is used in the subsequent reference to the kernel source directory used. PWD: = $ (shell pwd), which is assigned to the PWD variable, is to assign the return result of the $ (shell pwd) to the PWD, which we refer to as the driver where we want to compile the current directory. $ (make)-C $ (Kerneldir) m=$ (PWD) modules This is the makefile rule: the $ (make) here is the equivalent of the MAKE,-C option to transfer the current working directory to the location you specify. The function of the "m=" option is that when a user needs to compile an external module based on a kernel, it is necessary to add "M=dir" to the Make Modules command, and the program will automatically find the module source code in the dir directory you specify, compile it, and generate the Ko file. |