看板 LinuxDev 關於我們 聯絡資訊
※ 引述《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
andytzeng:w大該不會之前是在 2.4 上面寫 driver 吧..!! 04/13 10:39
wlsabcd:早上趕著上班,程式有誤,下班之後再補 04/13 11:20
※ 編輯: wlsabcd 來自: 203.73.175.134 (04/13 20:12)
wlsabcd:錯誤地方以補足 04/13 20:12
wlsabcd:我之前寫driver是為了project需求,還沒有module的觀念 04/13 20:13
wlsabcd:現在感覺完全是新手一樣 04/13 20:14
wlsabcd:我上上篇是指我之之前寫linux driver時,linux還沒導入 04/13 20:54
wlsabcd:module的觀念 04/13 20:55