In Linux 2.6, kbuild is used to compile the kernel module. Kbuild can compile the kernel modules in the kernel tree directory and the kernel modules (external kernel modules) outside the kernel tree directory ).
. Compile the command of the external kernel module:
# Cd <your-module-dir>
# Make-C <path-to-kernel> M = 'pwd'
<Your-module-dir> is the directory where the kernel module to be compiled is located, and <path-to-kernel> is the directory where the kernel source code is located.
For Linux of the released version, you can use:
# Make-C/lib/modules/'uname-R'/build M = 'pwd'
Note: Before using kbuild, you must compile the kernel source code first.
Note:
. # Make-C <path-to-kernel> M = 'pwd' modules
Same as the preceding command
. Earlier kernel versions can be used
# Make-C <path-to-kernel> subdirs = 'pwd' modules
. Install the external kernel module
# Make-C <path-to-kernel> M = 'pwd' modules_install
The default installation directory is/lib/modules/'uname-R'/extra. You can use the install_mod_path macro to add a prefix before the default installation path.
For example:
# Make-C <path-to-kernel> install_mod_path =/opt m = 'pwd' modules_install
The compiled modules are stored in/opt/lib/modules/'uname-R'/extra.
You can use the macro install_mod_dir to modify whether to put it under 'extra '. For example:
# Make-C <path-to-kernel> install_mod_dir = golf M = 'pwd' modules_install
The compiled modules are stored in/lib/modules/'uname-R'/golf.
. Compile a single file
# Make-C <path-to-kernel> M = 'pwd' <FILENAME>
. Other commands
# Make-C <path-to-kernel> M = 'pwd' clean
# Make-C <path-to-kernel> M = 'pwd' help
. Kbuild File
In Linux, kbuild searches for the kbuild file under the kernel module directory. If there is a kbuild file, it will be used during compilation.
Example:
Suppose there are several files: 8123_if.c 8123_if.h 8123_pci.c 8123_bin.o_shipped (Binary module File)
Content of the kbuild file:
OBJ-M: = 8123.o
8123-y: 8123_if.o 8123_pci.o 8123_bin.o
Makefile content:
# To be compatible with earlier versions of kbuild
Ifneq ($ (kernelrelease ),)
Include kbuild
Else
# Normal makefile
Kdir: =/lib/modules/'uname-R'/build
ALL ::
$ (Make)-C $ (kdir) M = 'pwd' $ @
# Other targets
Genbin:
Echo "X"> 8123_bin_shipped
Endif
Note: Binary. O files without source code must end with the original file name _ shipped, for example, 8123_bin.o_shipped. kbuild will ship 8123_bin.o_shipped
Copy it to 8123_bin.o and compile it together.
How to include your own include files in. makefile
When the external kernel module is compiled using kbuild, the compiling path is switched to the directory of the kernel source code tree. Therefore, if the relative path is used in the makefile to include another file, the file cannot be found. Therefore
Include ../config. mk
Use:
Ifeq ($ (OBJ ),)
OBJ =.
Endif
Include $ (OBJ)/../config. mk
Reprinted:
Http://blog.chinaunix.net/space.php? Uid = 1887234 & Do = Blog & cuid = 430023