Linux driver development kernel module compilation-Makefile getting started tutorial, driver development makefile
I. Module Compilation
In the previous kernel compilation, we mentioned that driver compilation is dividedStatic CompilationAndDynamic CompilationStatic compilation is to directly compile the driver into the kernel, and dynamic compilation is to compile the driver into a module.
Dynamic compilation can be divided into two types:
A -- Internal Compilation
Compile in the kernel source code directory
B -- External Compilation
Compile outside the kernel source code directory
Ii. detailed compilation process analysis
Note: This compilation is an external compilation. The kernel source code used is the Ubuntu source code, instead of the linux 3.14 kernel source code used by the Development Board. The operating platform is X86.
For a common linux device driver module, the following is a classic makefile code. Use the makefile below to compile most of the drivers, you only need to modify the name of the driver to be compiled. You only need to modify the obj-m value.
Ifneq ($ (KERNELRELEASE ),) Obj-m: = hello. o Else KDIR: =/lib/modules/$ (shelluname-r)/build PWD: = $ (shellpwd) All: Make-C $ (KDIR) M = $ (PWD) modules Clean: Rm-f *. ko *. o *. symvers *. cmd *. cmd. o Endif |
1. Variables in makefile
First, describe the meanings of some variables in the makefile below:
(1) KERNELRELEASEIt is defined in the top-level makefile in the Linux kernel source code.
(2) shell pwdGet current job path
(3) shell uname-rObtain the version number of the current Kernel
(4) KDIRThe source code directory of the current kernel.
There are two directories for linux source code:
"/Lib/modules/$ (shell uname-r)/build"
"/Usr/src/linux-header-$ (shell uname-r )/"
However, if the kernel is compiled, we will know that the source code under the usr directory is generally downloaded and decompressed by ourselves, while the source code under the lib directory is automatically copied during compilation, the file structure is the same, so sometimes the kernel source code directory is set to/usr/src/linux-header-$ (shell uname-r )/. You can modify the kernel source code directory based on your storage location.
(5) make-C $ (LINUX_KERNEL_PATH) M = $ (CURRENT_PATH) modules
This is the compilation module:
A -- firstChange the directory to the location specified by the-C option (that is, the kernel source code directory)), Where the top-level makefile with the kernel is saved;
B--M = option: Let the makefile return to the module source code directory before constructing the modules target.; Then,The modueles target points to the module set in the obj-m variable.In the above example, we set the variable to hello. o.
2. Steps for executing make
A --When I first came in, the macro "KERNELRELEASE" was not defined, so it entered the else;
B: record the kernel path and the current path;
Since make does not have a target, make will execute the first target in Makefile not starting with "." as the default target. The all rule is executed by default.
C -- make-C $ (KDIR) M = $ (PWD) moduleS
-C enters the kernel directory and executes Makefile. During the execution, KERNELRELEASE is assigned a value. M =$ (PWD) indicates that the current directory is returned and makefile is executed again., The meaning of modules compiling into a module
So what is actually running here is
Make-C/lib/modules/2.6.13-study/build M =/home/fs/code/1/module/hello/modules
D -- execute the makefile again, KERNELRELEASE has a value, and the obj-m: = hello. o will be executed.
Obj-m: Links hello. o and other target files to the hello. ko module File. during compilation, you must first compile hello. c into a hello. o file.
It can be seen that make has called three times in total.
1) -- make
2) -- the top-level makedile call of the Linux kernel source code tree is generated. O file
3) -- call the makefile of the Linux kernel source code tree and link the. o file to the ko file.
3. Compile multiple files
If multiple source files exist, use the following method:
Obj-m: = hello. o
Hello-objs: = file1.o file2.o file3.o
Iii. Brief description of internal Compilation
If you move the hello module to the kernel source code. For example, put it in/usr/src/linux/driver/, and the KERNELRELEASE is defined.
In/usr/src/linux/Makefile, KERNELRELEASE =$ (VERSION). $ (PATCHLEVEL). $ (SUBLEVEL) $ (EXTRAVERSION) $ (LOCALVERSION ).
At this time, the hello module is no longer compiled with make, but compiled with make modules in the kernel. At this time, the driver module is compiled with the kernel.