Linux kernel Module (driver) compile detailed __linux

Source: Internet
Author: User

This article mainly talk about how to compile your own development of the kernel module. Because drivers are often compiled into kernel modules, the content of the article also applies to drive compilation.

Because the ability is very limited, there are improper, but also hope that we criticize ^_^


First, preparatory work



There is no telling what to do about the preparation work.

a first of all, you have to have a PC (this is not nonsense ^_^), installed Linux.

(b) Install GCC (this refers to the host GCC, used to compile the build run on the PC program), make, ncurses and other tools.

c If you are developing a kernel module for your current PC, then the preparation is over.

If you are developing a kernel module for an embedded system Linux system, continue with the following two steps .

D Install the Cross compilation tool chain. For example, your target single board CPU may be arm or MIPS CPU, then install the corresponding cross-compilation tool chain. After installation, you need to add the tool chain path to the PATH environment variable. For example, if you have an ARM tool chain installed, then you execute a command similar to the following in your shell, and if you have a similar output, you are ready to install it.

[Root@localhost linux-2.6.33.i686]# ARM-LINUX-GCC--version
ARM-LINUX-GCC (buildroot 2010.11) 4.3.5
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software;  The source for copying conditions. There is NO
Warranty Not even for merchantability or FITNESS for A particular purpose.


e If you are compiling modules for embedded systems, download the required kernel source code, configure it, and compile it once. Otherwise, the module cannot be compiled. If you are unfamiliar with how to configure the build kernel, you can refer to the following article:

http://blog.csdn.net/crazycoder8848/article/details/44131735


Second, compiling

compile the module, in a way, like the next two methods.

(1) Independent compilation

(2) Integrate the module code into the kernel source package and follow the Linux kernel to compile


because of the multiple scenarios involved, the following are described separately.

For the sake of convenience, this assumes that we are compiling the module name Hello, and the module's C file directory is/root/hello.


(1) Independent compilation

First, create a makefile in the /root/hello .

if the module has only one. c file, then the filename must be hello.c.

at this point, the makefile content is super simple, all content is as follows line:

Obj-m: = hello.o


If the module is composed of multiple. c files, for example, MAIN.C,A.C,B.C.

at this point, the full contents of makefile are as follows:

Obj-m: = hello.o
HELLO-OBJS: = MAIN.O a.o b.o

in order to adapt to the more general situation, this makefile to do some explanation.

obj-m is a makefile variable whose value can be a table column of. o files.

each item in the list, representing a module.

In fact, we can compile multiple modules through a makefile.

For example, assume that the Hello directory has multiple modules of C files, not Hello, Hello2, Hello3.

the composition of the Hello module: main.c a.c B.C

Composition of Hello2 module: main2.c a2.c b2.c

Composition of Hello3 module: hello3.c

at this point, makefile written in the following form can be.

Obj-m: = hello.o hello2.o hello3.o
HELLO-OBJS: = MAIN.O a.o b.o
HELLO2-OBJS: = main2.o a2.o b2.o


with the makefile, you can compile it.

However, the module and the kernel are always matched. Compile the module, always inseparable from the core source code.

Therefore, we first need to determine the kernel source of the path. This is divided into two situations.

(i) Embedded Systems

since it is the source of their own download, of course, know the path.

(ii) Current PC system

for the current PC system, we did not download the source code for the previous preparation work. This is why. Because the PC installed Linux, a kernel source tree has been installed. The kernel source tree, which is not a complete core source, contains the kernel-sourced dock files, configuration files, and other information needed to compile the kernel modules. As a result, this is sufficient for compiling modules. We can determine the current PC kernel source tree path through the following methods.

First execute the uname-r command to get the version number of the kernel running on the current PC.

Suppose the output is: 2.6.33.3-85.fc13.i686.pae

then the kernel source tree path is

/lib/modules/2.6.33.3-85.fc13.i686.pae/build

at the same time, you can know that the current PC system, all modules are installed to the following path.

/lib/modules/2.6.33.3-85.fc13.i686.pae/kernel

So, with this information, you can start compiling.

a) Compile command:

Make-c /path/to/kernel_src_dir subdirs=/root/hello Modules

If you just compile the module for the kernel that is running for the current PC, and the shell's current working directory is/root/hello, the command can be simplified to (a similar simplification can be done with the following module installation commands):

Make-c/lib/modules/' uname-r '/build subdirs= ' pwd ' modules


(b) Module installation command: (improper typesetting, resulting in the command to change the line ^_^)

Suppose this module is a NIC driver.

(i) installing modules for the current PC

make-c/path/to/kernel_src_dir Subdirs=/root/hello install_mod_dir=kernel/drivers/net modules_install

After the above command is executed, the module is installed into the/lib/modules/2.6.33.3-85.fc13.i686.pae/kernel/drivers/net directory.

(ii) Installing modules for embedded systems. Suppose kernel release version is X

Make-c/path/to/kernel_src_dir Subdirs=/root/hello Install_mod_path=/path/to/rootfs_dir modules_install

After the above command is executed, the module is installed into the/path/to/rootfs_dir/lib/modules/x/kernel/drivers/net directory.


c) Clear command:

Make-c /path/to/kernel_src_dir Subdirs=/root/hello Clean

Finally, in order to compile the convenience, we add a easy_make under the/root/hello, the content is as follows.


BASEDIR: =/lib/modules/$ (Shell uname-r)
Kernel_src_tree? = $ (BASEDIR)/build
PWD: =$ (Shell PWD)
Install_dir? = kernel/drivers/net

. Phony:all
All:clean Modules Install

Obj-m: = hello.o hello2.o hello3.o
HELLO-OBJS: = MAIN.O a.o b.o
HELLO2-OBJS: = main2.o a2.o b2.o

. Phony:modules
Modules
$ (make)-C $ (kernel_src_tree) subdirs=$ (PWD) modules
@echo $ (abc-y)


. Phony:clean
Clean
$ (make)-C $ (kernel_src_tree) subdirs=$ (PWD) Clean


. Phony:install
Install
$ (make)-C $ (kernel_src_tree) subdirs=$ (PWD) install_mod_dir=$ (install_dir) Modules_install


In this way, we will only need CD to the/root/hello directory, the simple implementation of the following command, you can complete the corresponding operation ^_^

make-f easy_make Modules

Make-f easy_make Install make-f easy_make Clean

The above makefile default is to compile the module for the current PC. If you want to use him to compile the module for the embedded system, you can modify the value of the Kernel_src_tree and install_dir in it, or pass it directly from the command line.

(2) Integration of module code into the kernel source package is not finished ...



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.