> 文章列表 > Linux驱动之在Ubuntu下编译驱动模块——学习笔记(12)

Linux驱动之在Ubuntu下编译驱动模块——学习笔记(12)

Linux驱动之在Ubuntu下编译驱动模块——学习笔记(12)

为了方便驱动开发学习,了解一下在Ubuntu上进行驱动编译的流程。


一、下载对应的内核源码

首先要通过 uname -a查询一下自己的内核版本。
在这里插入图片描述
我这里下载的是

https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.4.tar.gz

Linux驱动之在Ubuntu下编译驱动模块——学习笔记(12)

二、编译内核

(1)编译依赖安装:

sudo apt-get install libncurses-dev
sudo apt-get install flex
sudo apt-get install bison
sudo apt-get install libelf-dev

否则会出现以下问题:
libncurses-dev引起:

  HOSTCC  scripts/basic/fixdep
*
* Unable to find the ncurses package.
* Install ncurses (ncurses-devel or libncurses-dev
* depending on your distribution).
*
scripts/kconfig/Makefile:208: recipe for target 'scripts/kconfig/mconf-cfg' failed
make[1]: *** [scripts/kconfig/mconf-cfg] Error 1
Makefile:567: recipe for target 'menuconfig' failed
make: *** [menuconfig] Error 2

flex: not found 引起:

/bin/sh: 1: flex: not found
scripts/Makefile.host:9: recipe for target 'scripts/kconfig/lexer.lex.c' failed
make[1]: *** [scripts/kconfig/lexer.lex.c] Error 127
Makefile:567: recipe for target 'menuconfig' failed
make: *** [menuconfig] Error 2

bison: not found引起:

/bin/sh: 1: bison: not found
scripts/Makefile.host:17: recipe for target 'scripts/kconfig/parser.tab.h' failed
make[1]: *** [scripts/kconfig/parser.tab.h] Error 127
Makefile:567: recipe for target 'menuconfig' failed
make: *** [menuconfig] Error 2

libelf-dev引起:

  CALL    scripts/checksyscalls.shCALL    scripts/atomic/check-atomics.sh
warning: Cannot use CONFIG_STACK_VALIDATION=y, please install libelf-dev, libelf-devel or elfutils-libelf-develCHK     include/generated/compile.h

(2)问题解决:

问题1:
make[1]: *** No rule to make target 'debian/canonical-certs.pem', needed by 'certs/x509_certificate_list'.  Stop.
Makefile:1652: recipe for target 'certs' failed
make: *** [certs] Error 2

解决:
通过 make menuconfig 配置删除 SYSTEM_TRUSTED_KEYS 的参数 debian/canonical-certs.pem

(3)清理及编译内核:

#清除编译过程中产生的所有中间文件
make mrproper
#清除上一次产生的编译中间文件
make distclean
#出现选择的图形化界面后,对内和编译选项进行配置,如果没有修改默认save生成.config即可
make menuconfig
#编译,这里用8线程进行编译
make -j8
#编译内核镜像
make bzImage

三、驱动编写及验证

(1)编写驱动程序以及Makefile:

参考:Linux驱动之简单入门——学习笔记(1)

(2)查看结果

开个端口实时查看内核输出:

sudo cat /proc/kmsg

通过insmod、rmmod等函数对编译出来的.ko文件进行加载及卸载。