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