I wrote several Linux kernel modules, but every time I wrote the code in a source file, I tried to write it in two. c files last time, and the result was not compiled.
In desperation, rename one of the. c files to a. h file, and include the file to another file. However, it is strange to Write Functions in the. h file.
Today, I checked the following kbuild documents, which are described as follows:
If a kernel module is built from several source files, you specify
That you want to build a module in the same way as above.
Kbuild needs to know which the parts that you want to build your
Module from, so you have to tell it by setting
$ (<Module_name>-objs) variable.
Example:
# Drivers/ISDN/i4l/makefile
OBJ-$ (config_isdn) + = ISDN. o
ISDN-objs: = isdn_net_lib.o isdn_v110.o isdn_common.o
In this example, the module name will be ISDN. O. kbuild will
Compile the objects listed in $ (ISDN-objs) and then run
"$ (LD)-R" on the list of these files to generate ISDN. O.
Organize your source files into two. c files (simplb. C and sahu_lb_tools.c), one. h file (sahu_lb.h), and makefile. Both. c files contain sahu_lb.h
According to the kbuild document, change makefile to the following content:
OBJ-M + = simplb. o
Simplb-objs: = sahu_lb_tools.o
ALL:
Make-C/lib/modules/'uname-R'/build M = 'pwd'
Clean:
Make-C/lib/modules/'uname-R'/build M = 'pwd' clean
Install:
/Sbin/insmod simplb. Ko
Remove:
/Sbin/rmmod simplb
There is no problem with compilation, but the functions of the module are not implemented after installation, even the prompt information I printed in init_module () does not exist. Lsmod has simplb.
I had to check it online and found the following article:
Http://www.linuxquestions.org/questions/programming-9/linking-multiple-files-kernel-module-programming-701735/
According to the article's suggestions, I changed the makefile to the following content:
OBJ-M + = sahulb. o
Sahulb-objs: = simplb. O sahu_lb_tools.o
ALL:
Make-C/lib/modules/'uname-R'/build M = 'pwd'
Clean:
Make-C/lib/modules/'uname-R'/build M = 'pwd' clean
Install:
/Sbin/insmod sahulb. Ko
Remove:
/Sbin/rmmod sahulb
Solve the problem!