Today the Linux device Driver (third edition) of the first module Hello module compiled passed, this thing card for me for a long time, during which I spent a lot of time to see the Linux Program Design (second edition), finally today the mechanical completion of this experiment.
Compilation environment: Virtual machine linux2.6.18 kernel, (if the kernel is not 2.6, you can refer to my kernel upgrade process, another article has a detailed record)
SOURCE program HELLO.C:
////////////////////////////////////////////////////////////////////////////
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
Module_license ("Dual BSD/GPL");
static int hello_init (void)//Some of the definitions above init_modules (void) are not compiled
{
PRINTK (Kern_alert "Hello, world/n");
return 0;
}
static void Hello_exit (void)
{
PRINTK (Kern_alert "Goodbye, world/n");
}
Module_init (Hello_init);
Module_exit (Hello_exit);
////////////////////////////////////////////////////////////////////////
Content of Makefile:
Ifneq ($ (kernelrelease),)
Obj-m: = hello.o
Else
kdir:=/lib/modules/$ (shell uname-r)/build
pwd:=$ (Shell PWD)
All
$ (make)-C $ (Kdir) subdirs=$ (PWD) modules
endif
Clean
Rm-f *.o *.ko *.mod.c. hello*
//////////////////////////////////////////////////////////
Put the hello.c makefile in the same folder Hello, in the Hello directory (my/home/leo/hello) will be prompted Hellomodules folders can not be found, create a hellomodules folder (Home/leo /hellomodules), and then in the Hello Directory (home/leo/hello) compile will prompt hello.c makefile can not find, hello.c makefile copy to Hellomodules directory down, and then compile OK.
Load module:
Insmod./hello.ko
(System hint: insmod command not found)
There are a lot of commands under the Linux virtual machine that can't be found because of path, we can use Whereis command to find, here
Whereis Insmod
(Find Insmod location)
(for example, in/usr/***/insmod, then use:)
/usr/***/insmod./hello.ko
(same changes to the system path can also be done)
After loading with the Lsmod command view, you can see that the Hello module has been loaded into the kernel, rmmod command usage is the same. (Note that rmmod Hello, not Hello.ko) to see the output of the information we can use: demsg | TAIL-N1 can see the output of "Hello World" and Bye
Information.