歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux服務器 >> Linux驅動Invalid module format錯誤

Linux驅動Invalid module format錯誤

日期:2017/3/2 16:48:55   编辑: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

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 錯誤。

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

module程序正常執行

Copyright © Linux教程網 All Rights Reserved