The Linux kernel driver compiles multiple C files into one ko file. Each C file contains module_init, module_exit, and moduleinit.

Source: Internet
Author: User

The Linux kernel driver compiles multiple C files into one ko file. Each C file contains module_init, module_exit, and moduleinit.

Take two C files as an example:

Compile adc_device.c and adc_driver.c of adc_device.ko and adc_driver.ko into a ko file!

Method:

Step 1: Modify the C file
1. Remove the keyword static for the prototype of the xxx and yyy functions in module_init (xxx) and module_exit (yyy) in the adc_device.c file.
2. log out of the module_init (xxx) and module_exit (yyy) functions in the adc_device.c file.
3. Add the xxx and yyy functions declared with the keyword extern in the adc_driver.c file.
4. In the adc_driver.c file, the mmm And nnn function prototypes in module_init (mmm) and module_exit (nnn) are suitable for calling xxx and yyy functions.
Note: When multiple C files are compiled into a. ko file, module_init and module_exit in the C file will prompt redefinition,
Therefore, one ko can only have one module_init and one module_exit!

 

Step 2: Compile the Makefile file (key part)

Obj-m + = adc. o
Adc-objs: = adc_device.o adc_driver.o

In this way, the adc_device.c and adc_driver.c that should have been compiled into adc_device.ko and adc_driver.ko respectively

Compile it into an adc. ko file!


Driver problems in linux

Cross-Compilation

Multi-file compilation in Linux kernel module

In this case, makefile defines some macros, including: CC compiler name, INS installer, INSDIR installation path, library directory required by LIBDIR, and library name required by LIBS, SRC Source file name.

After knowing these macros, $ in a makefile references a variable (variable name on the left of the equal sign ).

Then,-c in the dependency indicates compilation, and no execution file is generated.-o indicates output execution file.

For example, the following example may not be able to run. You can view it yourself, which means:

OBJS = foo. o bar. o
CC = GCC
CFLAGS =-Wall-O-g
Myprog: $ (OBJS)
$ (CC) $ (OBJS)-o myprog
Foo. o: foo. c foo. h bar. h
$ (CC) $ (CFLAGS)-c foo. c-o foo. o
Bar. o: bar. c bar. h
$ (CC) $ (CFLAGS)-c bar. c-o bar. o

Related Article

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.