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 <module name.o> <1st src file.o> <2nd src file.o>.
The makefile will, once again, save us from having to get our hands dirty with compiling and linking the object files.
Here's an example of such a kernel module.
Example 2-8. start.c
/* * start.c - Illustration of multi filed modules */ #include <linux/kernel.h> /* We're doing kernel work */ #include <linux/module.h> /* Specifically, a module */ int init_module(void) { printk("Hello, world - this is the kernel speaking\n"); return 0; }
The next file:
Example 2-9. stop.c
/* * stop.c - Illustration of multi filed modules */ #include <linux/kernel.h> /* We're doing kernel work */ #include <linux/module.h> /* Specifically, a module */ void cleanup_module() { printk("<1>Short is the life of a kernel module\n"); }
And finally, the makefile: