本文已参与「新人创作礼」活动,一起开启掘金创作之路。
PC操作系统:Ubuntu 16.04 LTS 内核版本:4.15.0-45-generic 编译器:gcc 7.5.0
主要是方便验证一些驱动的小问题。
一、下载内核源码
1、查看Ubuntu当前所使用的内核版本
执行命令 uname -r 可以查询。
2、查看一下可以下载的内核源码版本
执行命令 apt-cache search linux-source 可以查询。
可以看到有4.15.0版本的源码可以下。
3、下载源码及解压
(1)执行命令 sudo apt-get install linux-source-4.15.0 进行下载。
(2)源码会被下载在 /usr/src/ 目录下。
(3)解压到指定目录
这里我解压到自己的 home 目录。
4、配置及编译
(1)配置
- 运行 make menuconfig 命令,提示需要安装 ncurses 然后再重新尝试。
hrx@hrx-pc:~/linux-source-4.15.0$ make menuconfig
HOSTCC scripts/basic/fixdep
*** Unable to find the ncurses libraries or the
*** required header files.
*** 'make menuconfig' requires the ncurses libraries.
***
*** Install ncurses (ncurses-devel) and try again.
***
scripts/kconfig/Makefile:202: recipe for target 'scripts/kconfig/dochecklxdialog' failed
make[1]: *** [scripts/kconfig/dochecklxdialog] Error 1
Makefile:547: recipe for target 'menuconfig' failed
make: *** [menuconfig] Error 2
- 安装 ncurses 。执行 sudo apt-get install ncurses-dev 命令之后再次 make menuconfig 。出现配置界面则一切顺利。
- 根据需要进行内核配置,配置完选择 Save 然后默认将配置存为 .config 。然后选择 Exit 退出配置界面。
(2)编译
- 可以运行 make help 查看可以进行 make 的命令以及作用。
- 编译内核使用 make bzImage ,在 make help 可以查得到。
- 根据提示安装 libelf-dev 和 libssl-dev,然后再 make bzImage 生成内核文件。
sudo apt-get install libelf-dev libssl-dev