編譯Linux驅動模組

2021-10-08 02:13:55 字數 1958 閱讀 8354

> xz -d linux-5.7.8.tar.xz

> tar -xf linux-5.7.8.tar

> cd linux-5.7.8/

> make menuconfig

執行make menuconfig 報錯

error 1

unable to find the ncurses package.

install ncurses (ncurses-devel or libncurses-dev

depending on your distribution).

you may also need to install pkg-config to find the

ncurses installed in a non-default location.

make[1]: *** [scripts/kconfig/makefile:210:scripts/kconfig/mconf-cfg] 錯誤 1

make: *** [makefile:588:menuconfig] 錯誤 2

error 1 解決

sudo apt-get update

sudo apt-get install ncurses-dev

error 2

lex scripts/kconfig/lexer.lex.c

/bin/sh: 1: flex: not found

make[1]: *** [scripts/makefile.host:9:scripts/kconfig/lexer.lex.c] 錯誤 127

make: *** [makefile:588:menuconfig] 錯誤 2

error 2 解決

sudo apt-get install bison

sudo apt-get install flex

之後就可以進行linux核心編譯了。

我們以usbtest.c檔案為例,路徑為/drivers/usb/misc/usbtest.c

> cp ~/linux-5.7.8/drivers/usb/misc/usbtest.c ~/usbtest

// 新增makefile

> cd ~/usbtest

> vim makefile

makefile:

obj-m:=usbtest.o #要生成的模組名

mymodule-objs:=module

kerneldir:=/lib/modules/$(shell uname -r)/build # 使用當前主機的核心目錄

# kerneldir := /home/ali/linux-5.7.8 # 指定核心編譯版本

pwd:=$(shell pwd)

make:=make

default:

$(make) -c $(kerneldir) m=$(pwd) modules

clean:

$(make) -c $(kerneldir) m=$(pwd) clean

> make 編譯.ko

檢視核心版本

cat /proc/version

載入模組$ sudo insmod usbtest.ko

檢視模組

$ lsmod | grep "usbtest

解除安裝模組

$ sudo rmmod usbtest.ko

Linux驅動模組編譯模板

hello.c檔案 include include static int hello init void static void hello exit void module init hello init module exit hello exit module author vedic mod...

Linux核心驅動 模組編譯技巧

export arch arm export cross compile usr arm arm linux export subdirs dir 有時候我們無法獲取裝置的核心原始碼或者獲得的原始碼與裝置中的核心版本不同,此時如果安裝核心模組會提示exec format error錯誤,同時會在dm...

驅動模組編譯Makefile

驅動模組編譯makefile如下 1 使用不同的開發板核心時,一定要修改kern dir 2 kern dir中的核心要事先配置 編譯,為了能編譯核心,要先設定下列環境變數 2.1 arch,比如 export arch arm64 2.2 cross compile,比如 export cross...