linux 驅動開發 Invalid module format 錯誤解決方案

來源:互聯網
上載者:User

我想,很多剛開始學習linux 驅動程式的朋友都和我一樣,遇到過類似的問題。
1. 編譯錯誤(其實這個問題和 Makefile有很大關係)
2. Invalid module format 錯誤
3. insmod: error inserting './hello.ko': -1 File exists

開發環境
核心版本: 2.6.22   (我下載的最新版本)
gcc     :  gcc (GCC) 4.1.2

hello.c 程式可以參考
http://dev.yesky.com/154/2621154.shtml

Makefile
gcc -D__KERNEL__ -DMODULE -DLINUX -I /usr/local/src/linux2.4/include -c -o hello.o hello.c

上面這種寫法適合 2.4 版本的核心, 在2.6下用這種寫法很可能導致許多編譯錯誤,
即使編譯通過也會產生 Invalid module format 錯誤。
至於原因請參考
http://blog.csdn.net/pottichu/archive/2007/11/19/1892203.aspx

2.6下正確的 Makefie 如下:

# Makefile2.6
ifneq ($(KERNELRELEASE),)
 #kbuild syntax. dependency relationshsip of files and target modules are listed here.
 mymodule-objs := hello.o
 obj-m := hello.o   
else
PWD  := $(shell pwd)
KVER ?= $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build
all:
 $(MAKE) -C $(KDIR) M=$(PWD)
clean:
rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions
endif

使用上面的 Makefie 同時也解決了 Invalid module format 錯誤

insmod: error inserting './hello.ko': -1 File exists 錯誤 解決

第一次insmod ./hello.ko 沒有輸出表示你的模組被正常載入了,後來的錯誤是你重複載入這個模組導致的
用命令 lsmod | grep hello 看看是不是有這個模組?
用命令 tail /var/log/messages
有沒有 hello,world

OK 我的第一個module 程式終於正常執行了。 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.