linux驅動設計----helloword

步驟一、編寫hello.c。
代碼如下:
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
  printk(KERN_INFO " Hello World enter/n");
  return 0;
}

static void hello_exit(void)
{
  printk(KERN_INFO " Hello World exit/n ");
}

module_init(hello_init);
module_exit(hello_exit);

步驟二、編寫makefile。
內容如下:
ifneq ($(KERNELRELEASE),)
obj-m := hello.o
# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif

步驟三、測試驅動的安裝、卸載。
執行過程:  
[root@51arm hello]# make  
輸出:
make -C /lib/modules/2.6.27.5-117.fc10.i686/build M=/mnt/hgfs/pub/code/kernel/hello modules
make[1]: Entering directory `/usr/src/kernels/2.6.27.5-117.fc10.i686'  
  CC [M] /mnt/hgfs/pub/code/kernel/hello/hello.o  
  Building modules, stage 2.  
  MODPOST 1 modules  
  CC /mnt/hgfs/pub/code/kernel/hello/hello.mod.o  
  LD [M] /mnt/hgfs/pub/code/kernel/hello/hello.ko  
make[1]: Leaving directory `/usr/src/kernels/2.6.27.5-117.fc10.i686'  

執行: insmod ./hello.ko 和 rmmod,沒有輸出任何內容!
[root@51arm hello]# insmod ./hello.ko
[root@51arm hello]# rmmod hello
爲什麼呢。
原來,如果不在字符模式下需要到 /var/log/messages中去看輸出結果的。
執行 tail /var/log/messages,看見輸出結果了!
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章