Linux3.0.4下编译helloworld内核驱动程序


看了Linux设备驱动程序第三版中说,要学习驱动编程,先要建立源码树。对照我的linux的Ubuntu版本下了一个 2.6.35的,在编译helloworld程序时提示无效字符'I',查了下发现是由于版本不匹配。后来干脆下了一个最新版的内核3.0.4,直接升级我的ubuntu内核。一切还比较顺序,一个一个命令执行下来顺利完成升级。最后在编译helloworld程序的时候居然没有打印出想要的效果。在/var/log/messages中根本没看到书上所说的结果啊。原来结果是打印到了/var/log/syslog文件中的。奇怪的是开始的时候把module_exit写成mudule_exit编译居然通过,只是使用insmod安装的module不能够使用rmmod来卸载,总是提示device or resource busy.不管怎样第一个程序终于编译完成了,万事开头难。

#include<linux/init.h>

#include<linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");


static int hello_init(void)
{
    printk(KERN_ALERT "Hello World"\n);
    return 0;
}


static void hello_exit(void)
{
    printk(KERN_ALERT "Goodbye,Cruel World\n");
}


module_init(hello_init);

module_exit(hello_exit);

编译该helloworld的makefile文件是:

obj-m := helloworld.o
KERNELDIR := /lib/modules/3.0.4/build
PWD := $(shell pwd)
modules:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
    rm -rf *.o *~core .depend .*.cmd *.ko *.mod.c .tmp_versions $(TARGET)

相关内容