You can skip this section if you are using a lab machine, or if your own machine has the necessary kernel sources already installed. (Red Hat Fedora, for instance, generally has the necessary parts of the kernel sources installed by default.) To check whether your machine already has the files you need, just try compiling your module as above. If it works, you're all set. Otherwise, read on.
The site http://www.kernel.org is the primary source for Linux kernel source code; however, most distributions of Linux come with their own package management systems which can be used to automatically install the correct revision of the kernel source code (i.e. the source code for the revision of the kernel that is used in that version of the distribution). For instance, Red Hat's package manager, rpm, is used in Fedora and several other distributions. It may be easier to install the kernel source using your distribution's package management system, especially if you installed it from a pressed set of CDs and not one you burned yourself (because the kernel source package is probably not on the first CD, which is probably the only one you burned). The directions for doing this vary by distribution, but in Fedora you would issue a command like rpm -i kernel-devel-2.6.xx-x.xxx.i686.rpm where the xx are version numbers specific to which release of Fedora is being used and whether the kernel has been updated since that release was made.
If you can't or don't want to use the package management system, then you can download the kernel source directly from http://www.kernel.org. (These kernels are called "vanilla" kernels because they have not been patched by and are not specific to any particular distribution.) You'll want to get the same version as your machine is currently running; to find out, run uname
-r. Vanilla kernels have only three version numbers: the major version (2 for all Linux kernels since 1996 or so), the minor version (6 for recent kernels), and the release number (generally two digits). Non-vanilla kernels often have more version information added, usually after a dash. This "extra version" information will not be listed at the kernel.org site, so you will want the vanilla version corresponding to the full version your machine is running.
Once you have a Linux source archive from kernel.org, you'll want to extract it just like any other .tar.gz file (see the lab materials section above). However, by convention, Linux kernel source is extracted in the /usr/src directory, so you should put the archive there and extract it there. Note that you may need to be root to do this.
Once the kernel is extracted, cd to the kernel source directory and open the Makefile. You need to add the "extra version" information you got above to the line starting with EXTRAVERSION=. Save the file and then run make menuconfig. This will compile and run a small program in the kernel source tree which allows you to configure the many compile-time options the kernel has. Once the program starts, just quit it saving the configuration, as you won't actually be compiling the kernel. You just need to create the default configuration file. The next step is to run make
modules. You don't actually have to wait for all the modules to be built -- you only need the first few steps to happen. Once you see a lot of lines starting with CC [M], you can press control-C to kill this process.
Now your kernel tree has been primed so that it is ready to compile modules against, you need to do one last thing so that your module will be able to find the right kernel source tree. Again as root, you'll need to cd to /lib/modules and then to the directory named the same thing as your currently running kernel version (check uname -r). In this directory, there will probably not be a file named build (otherwise, when you tried to compile your wwrd module earlier, it probably would have worked). If there is a file named build, use mv to rename it, then issue this command: ln -s /usr/src/linux-2.6.xx build where xx is the version of the vanilla kernel you downloaded.
Now you should be able to compile your kernel module!