Detailed linux kernel module Makefile simple routines and compile the kernel module

      Kernel module is equivalent to the kernel patch, you can install the kernel into the kernel is running. As described in a simple hello.c, which is the source of a kernel module.

#include <linux/module.h>
#include <linux/init.h>

static int __init hello_init(void)
{
	printk(KERN_EMERG "Hello world!\n");
	return 0;
}

static void __exit hello_exit(void)
{
	printk(KERN_EMERG "hello exit\n");
}

module_init(hello_init);
module_exit(hello_exit);

In this code, defines two functions, namely hello_init and hello_exit, these two functions are printed some information. Ordinary print function c language is different here is the printk function to print. printk is a function exclusively as core print. The last two lines of code for these two functions are defined as a function of executing functions and kernel modules kernel module initialization phase of the implementation of the exit time.

       To compile the source code, we wrote the following Makefile file

KDIR := /lib/modules/4.10.0-28-generic/build

obj-m := hello.o

build: kernel_modules

kernel_modules:
	make -C $(KDIR) M=$(PWD) modules

clean:
	make -C $(KDIR) M=$(PWD) clean

In this document, we first define a variable KDIR, it defines a path, directory tool is used to compile the kernel modules. Which 4.10.0-28-generic kernel version number. If we want to install kernel modules in the kernel, the kernel must be compiled with the same compiler tool version number. You can use the following command to view the version number of the kernel.

uname -r

obj-m: = hello.o role of this line is specified after compilation module name.

build: the role of kernel_modules this line is to kernel_modules compiled this section to perform the operation.

Code kernel_modules this section is to make -C $ (KDIR) M = $ (PWD) modules

Including the role of -C $ (KDIR) is the directory where the specified compilation tools,

M = $ (PWD) is the directory specified by the source is located, which is equivalent to Run pwd PWD on the terminal, it returns the current directory, i.e. hello.c and Makefiel storage directory. The final modules specified compiled object is to generate a kernel module.

Makefile last two lines are clean code, the meaning is clear all files generated by the make process.

       Knowing this, we can begin to compile the kernel module. The make command, compile kernel modules. We can see the clip swells several files became available in the current file, which is what we hello.ko generated kernel module.

You may be implemented with the following command to install kernel modules.

insmod hello.ko

Using the following command to view the kernel modules already installed.

lsmod

Using the following command can unload kernel modules already installed.

rmmod hello

 

 

 

 

Released nine original articles · won praise 3 · Views 9767

Guess you like

Origin blog.csdn.net/bhniunan/article/details/104068085