这里我仅使用了最简单的代码HelloWorld
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_EMERG "Hello World enter!\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_EMERG "Hello world exit!\n");
}
module_init(hello_init);
module_exit(hello_exit);
//MODULE_AUTHOR("stone.shi");
//MODULE_DESCRIPTION("This is an example of programming driver!");
//MODULE_ALIAS("a simplest module”);
==============================================
[Makefile文件内容]
obj-m := hello.o
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD ?= $(shell pwd)
defalult:
make -C ${KERNELDIR} M=${PWD} modules
install:
insmod hello.ko
uninstall:
rmmod hello.ko
clear:
make -C ${KERNELDIR} M=${PWD} clean
****说明:
-C选项指定内核头文件的路径,所以如果这里不正确的话,就得仔细检查你的目录是不是正确。build只是一个符号链接,一般它指定的正是你的源码所在的目录,如果有的话。如果它不能正确链接,那么可能是你的源码并未安装或者安装的版本与当前正在工作的内核不对应。这时你就需要安装正确的与当前内核匹配的源码。
注意,Makefile的命令行要以TABLE开头,依赖条件顶格
==============================================
[编译]
好了,这时已经有两个文件了,hello.c和Makefile,随便找个目录放下,shell到这目录,输入#make:
MODPOST 1 modules
看到这个的话说明编译通过了,生成正确的内核模块文件hello.ko
===============================================
[加载]
执行#insmod hello.ko
[卸载]
#rmmod hellomod
[查看内核输出]
#tail /var/log/messages
这时就可以看到内核模块在加载和卸载时输出的信息了。
================================================
相关调试信息:
[root@localhost drivertest]# make
make -C /lib/modules/2.6.23.1-42.fc8/build M=/home/stone/drivertest modules
make[1]: Entering directory `/usr/src/kernels/2.6.23.1-42.fc8-i686'
CC [M] /home/stone/drivertest/hello.o
Building modules, stage 2.
MODPOST 1 modules
WARNING: "struct_module" [/home/stone/drivertest/hello.ko] undefined!
CC /home/stone/drivertest/hello.mod.o
LD [M] /home/stone/drivertest/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.23.1-42.fc8-i686'
[root@localhost drivertest]# ls
hello.c hello.ko hello.mod.o helloworld Makefile~ myproject1-0.9.10
hello.c~ hello.mod.c hello.o Makefile Module.symvers
[root@localhost drivertest]# insmod hello.ko
[root@localhost drivertest]#
Message from syslogd@localhost at Feb 17 19:50:45 ...
kernel: Hello World enter!
[root@localhost drivertest]# rmmod hello
[root@localhost drivertest]#
Message from syslogd@localhost at Feb 17 19:51:43 ...
kernel: Hello world exit!
[root@localhost drivertest]# vi hello.c
[root@localhost drivertest]# tail /var/log/messages
Feb 17 19:33:21 localhost gconfd (root-1949): 地址“xml:readonly:/etc/gconf/gconf.xml.mandatory”解析为位于 0 的只读配置源
Feb 17 19:33:21 localhost gconfd (root-1949): 地址“xml:readwrite:/root/.gconf”解析为位于 1 的可写入配置源
Feb 17 19:33:21 localhost gconfd (root-1949): 地址“xml:readonly:/etc/gconf/gconf.xml.defaults”解析为位于 2 的只读配置源
Feb 17 19:33:26 localhost pulseaudio[1965]: main.c: This program is not intended to be run as root (unless --system is specified).
Feb 17 19:33:26 localhost pulseaudio[1965]: pid.c: Daemon already running.
Feb 17 19:33:26 localhost pulseaudio[1965]: main.c: pa_pid_file_create() failed.
Feb 17 19:33:33 localhost gconfd (root-1949): 地址“xml:readwrite:/root/.gconf”解析为位于 0 的可写入配置源
Feb 17 19:34:11 localhost yum-updatesd-helper: error getting update info: Cannot retrieve repository metadata (repomd.xml) for repository: fedora. Please verify its path and try again
Feb 17 19:50:45 localhost kernel: Hello World enter!
Feb 17 19:51:43 localhost kernel: Hello world exit!
阅读(1604) | 评论(0) | 转发(0) |