linux驅動說開去 三 模組的編譯

2021-08-15 17:27:02 字數 4106 閱讀 4602

在linux驅動開發(一)--示例中我們將hello模組編譯為hello.ko是直接採用makefile來編譯,那麼如何修改為linux核心統一的風格呢,接下來將做相關介紹

關鍵點:

1.在menuconfig中新增子專案(kconfig)

2.在makefile中新增子專案的編譯(makefile)

kconfig背景知識:kconfig總結

在drivers目錄下建立hello目錄,上節示例中的hello.c,hello.h拷貝到hello目錄

root@leon:    /home/workplace/sc2410_android/kernel/drivers/hello# ls

hello.c hello.h

首先確定原先沒有hello test子選單:進入menuconfig內的device drivers子選單不存在hello test子項

root@leon:    /home/workplace/rk3229_android/kernel# make menuconfig
#

# hello device configuration

##menu "hello test"    //開啟則hello由子項改為子選單即內部可再支援新增子項

config hello //配置預編譯巨集

tristate "hello test"    //"hello test「對應menuconfig ->devices drivers內新增hello模組子項名,tristate為3態,檢視kconfig總結

default y    //3態預設選擇項,y為*,n為空

---help---    //hello子項對應help資訊

hello support test

#endmenu

hello的kconfig需要被上級目錄識別才能被引入:如下仿造新增即可

root@leon:    /home/workplace/sc2410_android/kernel/drivers# vim kconfig

menu "device drivers"

source "drivers/base/kconfig"

......

source "drivers/headset_observe/kconfig"

source "drivers/android/kconfig"

source "drivers/hello/kconfig"    //在上級drivers kconfig內新增引入hello/kconfig

endmenu

root@leon:    /home/workplace/sc2410_android/kernel/drivers/hello# ls

hello.c hello.h kconfig

驗證:再次進入menuconfig 中device drivers子選單,存在hello test子項

二.新增編譯項

root@leon:    /home/workplace/sc2410_android/kernel/drivers/hello# cat makefile 

## drivers/hello/makefile

## makefile for the linux hello device drivers.

#obj-$(config_hello) += hello.o    //config_hello預編譯巨集,與kconfig內對應,只是需要加config_字首

vi /home/workplace/sc2410_android/kernel/drivers/makefile

......

obj-$(config_hello) +=hello/

編譯hello.ko

root@leon:    /home/workplace/sc2410_android/kernel# make

scripts/kconfig/conf --silentoldconfig kconfig

chk include/generated/uapi/linux/version.h

chk include/generated/utsrelease.h

chk include/generated/sub_version.h

make[1]: 「include/generated/mach-types.h」是最新的。

call scripts/checksyscalls.sh

cc scripts/mod/devicetable-offsets.s

gen scripts/mod/devicetable-offsets.h

hostcc scripts/mod/file2alias.o

hostld scripts/mod/modpost

chk include/generated/compile.h

chk drivers/gator/gator_src_md5.h

cc [m] drivers/gator/gator_main.o

ld [m] drivers/gator/gator.o

ld drivers/hello/built-in.o

cc [m] drivers/hello/hello.o

gen drivers/video/rockchip/screen/lcd.h

ld drivers/built-in.o

link vmlinux

ld vmlinux.o

modpost vmlinux.o

objcopy pie/pie_stage1.o

ld pie/pie_stage2.o

objcopy pie/pie_stage3.o

ld pie/pie.elf

objcopy pie/pie.bin

objcopy pie/pie.bin.o

ld pie/built-in.o

gen .version

chk include/generated/compile.h

upd include/generated/compile.h

cc init/version.o

ld init/built-in.o

ksym .tmp_kallsyms1.o

ksym .tmp_kallsyms2.o

ld vmlinux

sortex vmlinux

sysmap system.map

objcopy arch/arm/boot/image

kernel: arch/arm/boot/image is ready

lzo arch/arm/boot/compressed/piggy.lzo

as arch/arm/boot/compressed/piggy.lzo.o

ld arch/arm/boot/compressed/vmlinux

objcopy arch/arm/boot/zimage

kernel: arch/arm/boot/zimage is ready

building modules, stage 2.

modpost 5 modules

cc drivers/gator/gator.mod.o

ld [m] drivers/gator/gator.ko

cc drivers/hello/hello.mod.o

ld [m] drivers/hello/hello.ko 成功編譯出hello.ko

擴充套件:1.tristate 3態:在menyconfig中選擇「*「則靜態編譯進核心,選擇「m」則編譯為單獨的.ko,選擇「 」則不編譯,由鍵盤空格鍵切換

linux驅動說開去 二 模組安裝 除錯

關鍵點 1.常用安裝解除安裝命令 2.安裝解除安裝涉及的常用符號 3.常用除錯手段 一.安裝解除安裝命令 1.lsmod 命令格式 lsmod shell sc2410 box lsmod vcodec service 33766 0 live 0x000000002.insmod 命令格式 ins...

Linux核心驅動模組的刪除

今天看了 linux裝置驅動開發詳解 的第四章模組和第五章檔案。學長給了乙個的任務 刪除linux核心中的dm9000的驅動模組,重新編譯核心。這篇文章現在寫肯定還不夠完善,我之後我會再補全,所以先打乙個 的標記。首先,我從檔案中找到dm9000驅動模組所在位置,在 drives net下。於是,我...

Linux驅動程式的模組引數

模組引數 模組引數就是使用者在系統啟動或掛載模組時指定的引數值,相對於驅動程式它是全域性變數。通過module param 定義模組引數 module param name,type,perm name 即為引數名 type 引數的型別,可以是byte 存放在char型變數中 short ushor...