[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: compiling kernel module question



On Fri, Nov 25, 2005 at 05:36:26PM -0500, Amish Rughoonundon wrote:
> Hi,
> I have been trying to compile and insert a simple kernel module but
> without luck. This is what I did.
> Since the freshly installed debian sarge 3.1 distro did not have any
> source files under /usr/src, I di uname -a to make sure of the kernel
> version that is installed:
> Linux test 2.4.27-2-386 #1 Mon May 16 16:47:51 JST 2005 i686 GNU/Linux
> 
> and then I downloaded the kernel-source-2.4.27.tar.bz2, unziped and
> untarred it. I then copied this program from  a book into example.c:
> 
> #include <linux/kernel.h>
> #include <linux/module.h>
> #include <linux/init.h>
> static char __initdata hellomessage[] = KERN_NOTICE "Hello, world!\n";
> static char __exitdata byemessage[] = KERN_NOTICE "Goodbye, cruel world.\n";
> static int __init start_hello_world(void)
> {
>    printk(hellomessage);
>    return 0;
> }
> static void __exit go_away(void)
> {
>    printk(byemessage);
> }
> module_init(start_hello_world);
> module_exit(go_away);
> 
> I then compiled it using 
> gcc -DMODULE -D__KERNEL__ -I/usr/src/kernel-source-2.4.27/include -c example.c
> 
> 
> I tried inserting it into the kernel using 
> /sbin/insmod example.o 
> 
> but this is the message I got back:
> 
> example.o: kernel-module version mismatch
>         example.o was compiled for kernel version 2.6.0
>         while this kernel is version 2.4.27-2-386.

If you want to build kernel modules, you need to use the kernel headers
_as configured for your current kernel_. The generic header files which
come with the original kernel sources won't work...

For a stock debian kernel such as 2.4.27-2-386, it's probably easiest
to just install the respective packages

* kernel-headers-2.4.27-2-386  (or kernel-headers-2.4-386 for that
  matter, which depends on kernel-headers-2.4.27-2-386), and

* kernel-headers-2.4.27-2  (containing the header files common to all
  architectures, referenced via symlinks from within the -386 package).

Then set your include path to -I/usr/src/kernel-headers-2.4.27-2-386/include.

I'm not entirely sure how you got that 2.6.0 version into your module,
but I guess the following happened:  as there's no "version.h" in the
unconfigured kernel sources, the file /usr/include/linux/version.h
probably got pulled in instead (because it's on the standard include
path)...  However, these include files (though they're kernel headers,
too) belong to libc, and must not necessarily match the current kernel
version (in fact, I believe those in sarge are version 2.6.0 -- btw,
this is the package linux-kernel-headers).

If you're interested in what went wrong in your original attempt, you
could run just the preprocessor (-E), and grep for version.h in its output

gcc -DMODULE -D__KERNEL__ -I/usr/src/kernel-source-2.4.27/include -E example.c | grep version.h

I'd think you see something like "# 1 "/usr/include/linux/version.h" 1 3"...

Cheers,
Almut



Reply to: