Linux Kernel module programming: a solution for compiling multiple source files into one module

Source: Internet
Author: User

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!

 

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.