A detailed overview of the compilation steps for embedded Linux modules allows you to avoid detours

Today, I will directly take you to the linux module compilation. Of course, during the introduction, I will also add some necessary comments so that beginners can understand. The reason for this article is mainly because it may take longer to learn the whole process if you learn from books, because reading a book is a learning process, and this article is more like a training. Therefore, it is more practical and summarized. After reading this article, you will learn the basics of compiling a module and module makefile. And load (unload) modules, and view some knowledge of system messages. If you are already a master at compiling linux modules, you can also give pointers on where to say the deficiencies.

Step 1: Prepare the source code

First of all, we still have to write a module file that conforms to the linux format, so that we can start our module compilation. Suppose we have a source file mymod.c. Its source code is as follows:

mymod.c

1. #include /* Introduce macros related to modules*/

2. #include /* Introduce module_init() module_exit() function*/

3. #include /* Introduce module_param() */

4

5. MODULE_AUTHOR("Yu Qiang");

6. MODULE_LICENSE("GPL");

7

8. static int nbr = 10;

9. module_param(nbr, int, S_IRUGO);

10.

11. static int __init yuer_init(void)

12.{

13. int i;

14. for(i=0; i

15. {

16. printk(KERN_ALERT "Hello, How are you. %d", i);

17.}

18. return 0;

19.}

20.

21.static void __exit yuer_exit(void)

twenty two.{

23. printk(KERN_ALERT"I come from yuer's module, I have been unlad.");

twenty four.}

25.

26. module_init(yuer_init);

27. module_exit(yuer_exit);

Our source file is almost ready, this is the basic structure of a module under linux. Line 9 is to export our symbolic variable nbr. In this way, you can dynamically modify the value of this variable when you load the module. Will demonstrate later. The yuer_init() function will run when the module is loaded, and you can see whether our module is loaded successfully through the output result.

Step 2: Write Makefile

First, let's take a look at the source file of our Makefile, and then we will explain;

Makefile

obj-m := modules.o #The name of the module to be generated

modules-objs:= mymod.o #Generate the object file needed for this module name

KDIR := /lib/modules/`uname -r`/build

PWD := $(shell pwd)

default:

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

clean:

rm -rf *.o .* .cmd *.ko *.mod.c .tmp_versions

Now I will explain this Makefile. Please remember that it is the uppercase Makefile and not the lowercase Makefile;

obj-m: This variable is to specify which modules you want to claim the format of the module is obj-m := .o

modules-objs: This variable is used to describe the object file format requirements required by modules modules-objs :=

Remember: The name of the module cannot be the same as the name of the target file. For example, the module name cannot be taken as mymod here;

KDIR: This is the kernel compilation directory of the operating system we are running. That is, the environment needed to compile the module

M=: Specify the location of our source file

PWD: This is the current working path. $(shell) is a built-in function of make. Used to execute shell commands.

Step 3: Compile the module

Now we have prepared the source files and corresponding Makefiles we need. We can compile now. Enter the source file directory and enter make in the terminal

operation result:

make[1]: Entering directory `/usr/src/linux-headers-2.6.24-24-generic'

CC [M] /home/yuqiang/desktop/mymodule/mymodules.o

LD [M] /home/yuqiang/desktop/mymodule/modules.o

Building modules, stage 2.

MODPOST 1 modules

CC /home/yuqiang/desktop/mymodule/modules.mod.o

LD [M] /home/yuqiang/desktop/mymodule/modules.ko

make[1]: Leaving directory `/usr/src/linux-headers-2.6.24-24-generic'

Step 4: Load/unload our module

I can see from the above compilation. There is already a modules.ko generated. This is our module. Now we can load it.

First enter in the terminal: sudo insmod modules.ko

Now let's see if our module is loaded successfully?

Enter in the terminal: dmesg | tail -12 This is the meaning of viewing the output information of the kernel. tail -12 displays the last 12;

The display results are as follows:

[17945.024417] sd 9:0:0:0: Attached scsi generic sg2 type 0

[18046.790019] usb 5-8: USB disconnect, address 9

[19934.224812] Hello, How are you. 0

[19934.224817] Hello, How are you. 1

[19934.224818] Hello, How are you. 2

[19934.224820] Hello, How are you. 3

[19934.224821] Hello, How are you. 4

[19934.224822] Hello, How are you. 5

[19934.224824] Hello, How are you. 6

[19934.224825] Hello, How are you. 7

[19934.224826] Hello, How are you. 8

[19934.224828] Hello, How are you. 9

See it. The initialization function yuer_init(); of our module has been successfully run. It shows that our module has been loaded successfully;

Now let's try to uninstall the module again.

Enter in the terminal: sudo rmmod modules

Enter in the terminal: dmesg | tail -3

[19934.224826] Hello, How are you. 8

[19934.224828] Hello, How are you. 9

[20412.046932] I come from yuer's module, I have been unlad.

As you can see from the printed information, the exit function of our module has been executed. It shows that our module has been successfully uninstalled. Up to the present position, we already have an overall understanding of the compilation of the module to the compilation and running of the module. It should be a bit helpful for further study in the future. Below we will look at some simple operations related to the module.

Step 5: Pass parameters when loading the module

Enter in the terminal: sudo insmod module_name.ko nbr=4

Type in the terminal: dmesg | tail -6

The display results are as follows:

[20800.655694] Hello, How are you. 9

[21318.675593] I come from onefile module, I have been unlad.

[21334.425373] Hello, How are you. 0

[21334.425378] Hello, How are you. 1

[21334.425380] Hello, How are you. 2

[21334.425381] Hello, How are you. 3

So we can see that one of our variables is dynamically set when the module is loaded. The loop in the initialization function was executed only 4 times.

You may ask me how do I know which variables a module can set. Of course, you can load it once without setting variables. Then you can enter ls /sys/module//parameters/ in the terminal to view. Here we enter it like this

Type in the terminal: ls /sys/moedle/modules/parameters/

show result:

nbr

If our module is loaded successfully. Finally, we can also view our module information through modinfo. as follows

Enter in the terminal: sudo modinfo modules.ko

show result:

filename: modules.ko

license: GPL

author: Yu Qiang

srcversion: 20E9C3C4E02D130E6E92533

depends:

vermagic: 2.6.24-24-generic SMP mod_unload 586

parm: nbr:int

to sum up:

The relevant knowledge introduced today seems to be a little bit simple. Because here is mainly a linear way to explain the related process of module writing, in fact, there are many places that can diverge in this process. E.g:

When writing MODULE_AUTHOR("Yu Qiang"), you should think of

MODULE_DESCRIPTION (a brief description of the purpose of the module);

MODULE_VERSION (the version string of the module);

MODULE_ALIAS (the alias of the module);

...

When writing module_param(nbr, int, S_IRUGO);, you should think of

EXPORT_SYMBOL(name); The function of the module can be exported, which is also the ultimate goal of writing the module

...

When using insmod and modinfo. You should think there is

depmod analyzes the dependencies of loadable modules and generates modules.dep files and mapping files

modprobe Linux kernel add and delete modules

...

If you want to become a professional linux module developer, you have to go a lot. It's up to you. I wish you all the best in your studies.

Bang Vape

"BANG" is a brand of disposable vaping products and e-liquids that is sold in lots of countries.BANG's vape products are available from 600puffs to 8000 puffs, with various colors. We wholesale this brand, and you can contact us at any time if you need.

Bang Xxl 2000puffs

Bang Vape,Bang Disposable Vape,Bang Vape Kit,Bang Vape Pod,Bang Bar Disposable Pod

TSVAPE Wholesale/OEM/ODM , https://www.tsecigarette.com