Compile the kernel module

Source: Internet
Author: User

The method for compiling the kernel module is slightly different from that for compiling general applications. We will find that makefile exists in the directories of the kernel source code tree.
In other words, these makefiles are organized hierarchically. In previous kernel versions, compilation modules were troublesome and we needed to make many changes to these makefiles.
2.6 kernel uses the "kbuild" compiling system to simplify these problems. For details about kbuild, refer to/documentation/kbuild/modules.txt in the kernel source code tree.

Before compilation, the source file is required. these source files can be stored in the kernel source code tree or any place outside the kernel source code tree. based on the directory in the source file, there are two compilation methods: in the source code tree and out of the source code tree.

Compile the module in the source code tree

The source code of the official kernel module is organized by the module (driver) type. We can see the subdirectories such as char, USB, block in the DRIVERS directory of the kernel source code tree. when we add files to the kernel source code tree, we 'd better follow these categories. the classification rules are flexible.

The following is a simple "Hello, world" module. Let's take a look at how to compile the module in the kernel source code tree.

1. No subdirectory is created.
(1) first, edit a C source program named hello. C. In the DRIVERS directory in the kernel source code tree.
(2) modify the MAKEFILE file of the DRIVERS directory and add: obj-M + = Hello. o
(3) re-compile the kernel (return to the root directory of the source code and run $ sudo make ).

In this way, the DRIVERS directory contains the following files: Hello. mod. c, hello. mod. o, hello. o, hello. ko. hello. ko is the compiled module.

2. Create a subdirectory
If there are many source files, you can create a subdirectory In the DRIVERS directory or use "Hello, world" as an example:
(1) Create a New Hello subdirectory In the DRIVERS directory of the kernel source code tree and put Hello. c In the hello directory.
(2) modify the MAKEFILE file of the DRIVERS directory and add: obj-M + = Hello/
(3) create a new MAKEFILE file in the hello directory with the content obj-M + = Hello. o
(4) re-compile the kernel (return to the root directory of the source code and run $ sudo make ).

In this way, the newly generated module File is located in the hello directory.

If the module is compiled in the kernel source code tree, if no subdirectory is created, you only need to modify the makefile of the current directory. Otherwise, you should create a new makefile in the new subdirectory to specify the compilation option, modify the makefile of the upper directory so that kbuild can enter the new subdirectory.


Compile the module outside the source code tree

Take the preceding Hello, world as an example. There is a hello. C in the current directory:

(1) create a makefile in the directory where the module code is located. The content is:
OBJ-M: = Hello. o
(2) Call The make command as follows:
$ Sudo make-C/usr/local/src/kernel/linux-2.6.16.20 subdirs = $ PWD modules
Here/usr/local/src/kernel/linux-2.6.16.20 is the directory of the kernel source tree.

-C indicates that make must switch to the directory specified by-C. subdirs (you can also use M to replace subdirs) so that make can return to the current directory before compiling the module.

The entire compilation process is actually executing the makefile of the kernel source code tree specified by-C, and specifying the directory of the kernel source file to be compiled through subdir.

Simplified command line Input
It is troublesome to enter these parameters when calling make. You can rewrite makefile to simplify the process:

obj-m += hello.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

In this way, you only need to call $ sudo make in the current directory to complete the above work. Call $ sudo make clean to delete all newly generated files.

The makefile above is used to determine the directory where the kernel source code tree is located: We first go to the/lib/modules directory, and we will see some directories named after the kernel version,
There is a build file in the directory, which is a symbolic connection pointing to the kernel source code tree. How do you determine which kernel version to enter? This can be determined by $ uname-R, which indicates the current version of the running kernel.

You can further simplify this makefile:
OBJ-M: = Hello. o

Kerneldir? =/Lib/modules/$ (shell uname-R)/build
PWD: = $ (shell PWD)

Default:
$ (Make)-C $ (kerneldir) M = $ (PWD) Modules
Clean:
$ (Make)-C $ (kerneldir) M = $ (PWD) clean

In this way, you do not need to specify the directory of the kernel code tree again and again in makefile.

The above example only discusses the situation of all the code in one file. If the code is distributed in multiple source files, such as file1.c and file2.c, generate hello. KO. The makefile should be written as follows:
OBJ-M: = Hello. o
Hello-objs: = file1.o file2.o
Note: although our purpose is to generate the. Ko file, write it as. O in makefile!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.