本文共 5406 字,大约阅读时间需要 18 分钟。
宿主机 : 虚拟机 Ubuntu 16.04 LTS / X64
目标板[底板]: Tiny4412SDK - 1506 目标板[核心板]: Tiny4412 - 1412 LINUX内核: 4.12.0 交叉编译器: gcc-arm-none-eabi-5_4-2016q3 日期: 2017-8-8 21:52:29 作者: SY编译内核模块和编译内核类似,是将目标内核放在宿主机中编译,使用交叉编译器,就是借鸡生蛋
root@ubuntu:/opt/linux-4.12# make modules
方式一:默认安装路径
root@ubuntu:/opt/linux-4.12# make modules_install
默认安装位置/lib/modules/
root@ubuntu:/opt/linux-4.12# ls /lib/modules/4.12.0-g16504fa/build/ modules.builtin modules.devname modules.symbols.binkernel/ modules.builtin.bin modules.order source/modules.alias modules.dep modules.softdep modules.alias.bin modules.dep.bin modules.symbols
在这个目录有一个build
文件夹很有用,以后写一个独立的外部模块时,不用编译内核了,只要进入路径:/lib/modules/4.12.0-g16504fa/build
即可编译依赖于此内核的模块。
方式二:自定义路径
root@ubuntu:/opt/linux-4.12# make modules_install INSTALL_MOD_PATH=/opt/fs/rootfs/rootfs/root@ubuntu:/opt/fs/rootfs/rootfs/lib/modules# cd /opt/fs/rootfs/rootfs/lib/modules/4.12.0-g16504fa-dirty/root@ubuntu:/opt/fs/rootfs/rootfs/lib/modules/4.12.0-g16504fa-dirty# lsbuild modules.alias.bin modules.dep modules.order modules.symbols.binkernel modules.builtin modules.dep.bin modules.softdep sourcemodules.alias modules.builtin.bin modules.devname modules.symbols
手动指定模块安装路径,将内核模块安装到rootfs
中。
方式一:如果在menuconfig
中,配置为[M]
, 都会被编译。编译的文件名为xxx.ko
root@ubuntu:/opt/linux-4.12# make modules CHK include/config/kernel.release CHK include/generated/uapi/linux/version.h CHK include/generated/utsrelease.h CHK include/generated/bounds.h CHK include/generated/timeconst.h CHK include/generated/asm-offsets.h CALL scripts/checksyscalls.sh CHK scripts/mod/devicetable-offsets.h CC [M] drivers/tiny4412/hello_world.o Building modules, stage 2. MODPOST 39 modules LD [M] drivers/md/dm-crypt.ko CC drivers/tiny4412/hello_world.mod.o LD [M] drivers/tiny4412/hello_world.koroot@ubuntu:/opt/linux-4.12# cd drivers/tiny4412/root@ubuntu:/opt/linux-4.12/drivers/tiny4412# lshello_world.c hello_world.ko hello_world.mod.c hello_world.mod.o hello_world.o Kconfig Makefile modules.order
方式二:在内核源码的待编译的驱动目录,编译当前目录的模块。
root@ubuntu:/opt/linux-4.12# cd drivers/tiny4412/root@ubuntu:/opt/linux-4.12/drivers/tiny4412# lshello_world.c Kconfig Makefileroot@ubuntu:/opt/linux-4.12/drivers/tiny4412# make -C /opt/fs/rootfs/rootfs/lib/modules/4.12.0-ge0f7e9e-dirty/build M=$PWDmake: Entering directory '/opt/linux-4.12' LD /opt/linux-4.12/drivers/tiny4412/built-in.o CC [M] /opt/linux-4.12/drivers/tiny4412/hello_world.o Building modules, stage 2. MODPOST 1 modules CC /opt/linux-4.12/drivers/tiny4412/hello_world.mod.o LD [M] /opt/linux-4.12/drivers/tiny4412/hello_world.komake: Leaving directory '/opt/linux-4.12'root@ubuntu:/opt/linux-4.12/drivers/tiny4412# lsbuilt-in.o hello_world.ko hello_world.mod.o Kconfig modules.orderhello_world.c hello_world.mod.c hello_world.o Makefile Module.symvers
方式三:脱离linux
内核源码,在任意目录编译指定模块
root@ubuntu:/# cd /opt/temp/temproot@ubuntu:/opt/temp/temp# lshello_world.c Makefile
需要加入下面的Makefile
文件
# Linux modules compile# Author : SY# Time : 2017-8-8 21:30:31#############################################################################obj-m := hello_world.oCROSS := arm-none-eabi-KDIR := /opt/fs/rootfs/rootfs/lib/modules/4.12.0-ge0f7e9e-dirty/buildNPWD := $(shell pwd)INSTALL_DIR := /opt/fs/rootfs/rootfs/default: $(MAKE) -C $(KDIR) M=$(PWD)install: $(MAKE) -C $(KDIR) M=$(PWD) modules_install INSTALL_MOD_PATH=$(INSTALL_DIR)clean: rm -rf *.o *.ko *.mod.c *.temp_versions *.symvers *.order
root@ubuntu:/opt/temp/temp# makemake -C /opt/fs/rootfs/rootfs/lib/modules/4.12.0-ge0f7e9e-dirty/build M=/opt/temp/tempmake[1]: Entering directory '/opt/linux-4.12' LD /opt/temp/temp/built-in.o CC [M] /opt/temp/temp/hello_world.o Building modules, stage 2. MODPOST 1 modules CC /opt/temp/temp/hello_world.mod.o LD [M] /opt/temp/temp/hello_world.komake[1]: Leaving directory '/opt/linux-4.12'root@ubuntu:/opt/temp/temp# lsbuilt-in.o hello_world.ko hello_world.mod.o Makefile Module.symvershello_world.c hello_world.mod.c hello_world.o modules.order
root@ubuntu:/opt/temp/temp# make installmake -C /opt/fs/rootfs/rootfs/lib/modules/4.12.0-ge0f7e9e-dirty/build M=/opt/temp/temp modules_install INSTALL_MOD_PATH=/opt/fs/rootfs/rootfs/make[1]: Entering directory '/opt/linux-4.12' INSTALL /opt/temp/temp/hello_world.ko DEPMOD 4.12.0-ge0f7e9e-dirtymake[1]: Leaving directory '/opt/linux-4.12'
将会安装到路径/opt/fs/rootfs/rootfs/lib/modules/4.12.0-ge0f7e9e-dirty/extra
root@ubuntu:/opt/linux-4.12# cd /opt/fs/rootfs/rootfs/lib/modules/4.12.0-ge0f7e9e-dirty/extra/root@ubuntu:/opt/fs/rootfs/rootfs/lib/modules/4.12.0-ge0f7e9e-dirty/extra# lshello_world.ko
方式一:insmod
[root@TINY4412:/tmp]# insmod hello_world.ko
方式二:modprobe
[root@TINY4412:~]# modprobe hello_worldmodprobe: module 'hello_world.ko' not found[root@TINY4412:~]# depmod[root@TINY4412:~]# modprobe hello_world[14384.366786] ------------------- hello_world_probe
insmod
和modprobe
的区别:
insmod
:只能加载指定路径的驱动文件
modprobe
:只能加载/lib/modules/4.12.0-ge0f7e9e-dirty/
路径下的驱动文件。
[root@TINY4412:/tmp]# lsmod hello_world.ko hello_world 16384 0 - Live 0xbf008000 (O)
[root@TINY4412:/tmp]# rmmod hello_world.ko
转载地址:http://inzii.baihongyu.com/