作者wlsabcd (我不会C++)
看板LinuxDev
标题Re: [转录][问题]kernel跟驱动程式版本不合
时间Fri Apr 13 08:53:58 2007
※ 引述《andytzeng (Ya-Shiuan)》之铭言:
: 推 andytzeng:事实上..执行 make install 就会帮你 copy config file 04/08 18:35
: → andytzeng:再者, 2.6.18, 2.6.19, 2.6.20 许多 module 位置换地方 04/08 18:36
: → andytzeng:因此直接套用就会发现部分功能出问题(尤其 iptables) 04/08 18:36
感谢各位的帮忙,在此奉上一篇(最简的的Kernel module)以回报本版。
这只是入门而已,当然高手可以省略此篇
make a directory named as "module_begin"
Edit a make file as:
####################################################################
# Make file begine #
####################################################################
obj-m := hello.o
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
$(MAKE) -C $(KERNELDIR) M=$(PWD)
clean:
@rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions \
Module.symvers
####################################################################
# Make file End #
####################################################################
Edit hello.c as:
/***************************************************************************
* Hello.c Begin
***************************************************************************/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#define ENABLE_DMSG 1
#if(1 == ENABLE_DMSG)
#define MSG(X...) printk(KERN_ALERT __FILE__ ": " X)
#else
#define MSG(X...) do { } while (0)
#endif
static int __init hello_init(void)
{
MSG(KERN_ALERT "Hello Driver!\n");
return 0;
}
static void __exit hello_exit(void)
{
MSG(KERN_ALERT "Goodbye Driver!\n");
}
module_init( hello_init);
module_exit( hello_exit);
MODULE_LICENSE( "GPL" );
MODULE_AUTHOR( "MyName" );
/**************************************************************************
* Hello.c End
**************************************************************************/
type "make" command to make, the result ought to be
make -C /lib/modules/2.6.19-rc6/build M=/root/code/module_begin
make[1]: Entering directory `/usr/src/linux-2.6.18'
LD /root/code/module_begin/built-in.o
CC [M] /root/code/module_begin/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /root/code/module_begin/hello.mod.o
LD [M] /root/code/module_begin/hello.ko
make[1]: Leaving directory `/usr/src/linux-2.6.18'
use the command "insmod hello.ko" to install hello module,
then "dmesg" the messgae is
/root/code/module_begin/hello.c: <1>Hello Driver!
use the command "rmmod hello" to remove hello module,
the message is
/root/code/module_begin/hello.c: <1>Goodbye Driver!
P.S. 太久没写Linux driver了,没想到变了这麽多。
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 203.73.175.134
1F:推 andytzeng:w大该不会之前是在 2.4 上面写 driver 吧..!! 04/13 10:39
2F:→ wlsabcd:早上赶着上班,程式有误,下班之後再补 04/13 11:20
※ 编辑: wlsabcd 来自: 203.73.175.134 (04/13 20:12)
3F:推 wlsabcd:错误地方以补足 04/13 20:12
4F:→ wlsabcd:我之前写driver是为了project需求,还没有module的观念 04/13 20:13
5F:→ wlsabcd:现在感觉完全是新手一样 04/13 20:14
6F:→ wlsabcd:我上上篇是指我之之前写linux driver时,linux还没导入 04/13 20:54
7F:→ wlsabcd:module的观念 04/13 20:55