分类: LINUX
2006-07-01 08:29:16
Sometimes it makes sense to divide a kernel module between several source files. In this case, you need to:
In all the source files but one, add the line #define __NO_VERSION__. This is important because module.h normally includes the definition of kernel_version, a global variable with the kernel version the module is compiled for. If you need version.h, you need to include it yourself, because module.h won't do it for you with __NO_VERSION__.
Compile all the source files as usual.
Combine all the object files into a single one. Under x86, use ld -m elf_i386 -r -o
Here's an example of such a kernel module.
Example 2-8. start.c
/* start.c - Illustration of multi filed modules */ #include |
The next file:
Example 2-9. stop.c
/* stop.c - Illustration of multi filed modules */ #if defined(CONFIG_MODVERSIONS) && ! defined(MODVERSIONS) #include |
And finally, the makefile:
Example 2-10. Makefile for a multi-filed module
CC=gcc MODCFLAGS := -O -Wall -DMODULE -D__KERNEL__ hello.o: hello2_start.o hello2_stop.o ld -m elf_i386 -r -o hello2.o hello2_start.o hello2_stop.o start.o: hello2_start.c ${CC} ${MODCFLAGS} -c hello2_start.c stop.o: hello2_stop.c ${CC} ${MODCFLAGS} -c hello2_stop.c |