编译模块必须首先编译内核,生成模块编译过程中所需要的各种依赖文件,使用的系统是最新的ubuntu 16.04
编译内核首先得有内核的源代码,但是ubuntu默认安装是没有源代码的,需要自己安装
安装方法相当简单:sudo apt-get install linux-source
执行完上述命令后,linux源代码后会被安装到/usr/src目录下
linux-source-4.4.0.tar.bz2包是内核的源码包
解包:tar -xvf linux-source-4.4.0.tar.bz2
生成的linux源码目录结构:
root@home:/usr/src/linux-source-4.4.0/linux-source-4.4.0# ls -al
总用量 1655292
drwxrwxr-x 28 root root 4096 9月 13 22:39 .
drwxr-xr-x 5 root root 4096 9月 12 22:45 ..
drwxr-xr-x 34 root root 4096 9月 13 22:39 arch
drwxr-xr-x 3 root root 4096 9月 13 22:39 block
drwxr-xr-x 2 root root 4096 9月 13 22:39 certs
-rw-r--r-- 1 root root 189683 9月 12 22:51 .config
-rw-r--r-- 1 root root 189683 9月 12 22:51 .config.old
-rw-r--r-- 1 root root 18693 1月 11 2016 COPYING
-rw-r--r-- 1 root root 97181 1月 11 2016 CREDITS
drwxr-xr-x 4 root root 24576 9月 13 22:40 crypto
drwxr-xr-x 112 root root 12288 8月 12 03:52 Documentation
drwxr-xr-x 128 root root 4096 9月 13 22:39 drivers
-rw-r--r-- 1 root root 681 8月 12 02:01 dropped.txt
drwxr-xr-x 36 root root 4096 9月 13 22:50 firmware
drwxr-xr-x 74 root root 12288 9月 13 22:48 fs
drwxr-xr-x 30 root root 4096 9月 12 22:03 include
drwxr-xr-x 2 root root 4096 9月 13 22:39 init
drwxr-xr-x 2 root root 4096 9月 13 22:39 ipc
-rw-r--r-- 1 root root 2622 1月 11 2016 Kbuild
-rw-r--r-- 1 root root 252 1月 11 2016 Kconfig
drwxr-xr-x 16 root root 12288 9月 13 22:48 kernel
drwxr-xr-x 12 root root 24576 9月 13 22:48 lib
-rw-r--r-- 1 root root 338882 8月 12 02:01 MAINTAINERS
-rw-r--r-- 1 root root 55578 8月 12 02:01 Makefile
-rw-r--r-- 1 root root 485 9月 13 22:34 .missing-syscalls.d
drwxr-xr-x 3 root root 12288 9月 13 22:48 mm
-rw-r--r-- 1 root root 6969 9月 13 22:39 modules.builtin
-rw-r--r-- 1 root root 181223 9月 13 22:39 modules.order
-rw-r--r-- 1 root root 1217024 9月 13 22:40 Module.symvers
drwxr-xr-x 60 root root 4096 9月 13 22:39 net
-rw-r--r-- 1 root root 18599 1月 11 2016 README
-rw-r--r-- 1 root root 7485 1月 11 2016 REPORTING-BUGS
drwxr-xr-x 16 root root 4096 8月 12 03:52 samples
drwxr-xr-x 13 root root 4096 9月 12 22:53 scripts
drwxr-xr-x 9 root root 4096 9月 13 22:39 security
-rw-r--r-- 1 root root 467 8月 12 02:01 snapcraft.yaml
drwxr-xr-x 23 root root 4096 9月 13 22:50 sound
drwxr-xr-x 10 root root 4096 8月 12 03:51 spl
-rw-r--r-- 1 root root 3861122 9月 13 22:39 System.map
-rw-r--r-- 1 root root 4140208 9月 13 22:38 .tmp_kallsyms1.o
-rw-r--r-- 1 root root 4140208 9月 13 22:38 .tmp_kallsyms2.o
-rw-r--r-- 1 root root 3861122 9月 13 22:39 .tmp_System.map
drwxr-xr-x 2 root root 155648 9月 13 22:38 .tmp_versions
-rwxr-xr-x 1 root root 404628824 9月 13 22:38 .tmp_vmlinux1
-rwxr-xr-x 1 root root 408965056 9月 13 22:38 .tmp_vmlinux2
drwxr-xr-x 23 root root 4096 8月 12 03:51 tools
drwxr-xr-x 7 root root 4096 9月 13 22:39 ubuntu
drwxr-xr-x 2 root root 4096 9月 13 22:39 usr
-rw-r--r-- 1 root root 2 9月 13 22:38 .version
drwxr-xr-x 4 root root 4096 9月 13 22:39 virt
-rwxr-xr-x 1 root root 408965056 9月 13 22:39 vmlinux
-rw-r--r-- 1 root root 91 9月 13 22:39 .vmlinux.cmd
lrwxrwxrwx 1 root root 73 9月 13 22:38 vmlinux-gdb.py -> /usr/src/linux-source-4.4.0/linux-source-4.4.0/scripts/gdb/vmlinux-gdb.py
-rw-r--r-- 1 root root 472328760 9月 13 22:38 vmlinux.o
drwxr-xr-x 13 root root 4096 8月 12 03:52 zfs
可以看到Makefile文件,执行make menuconfig进入图形化界面进行配置,然后……报错了……
sean@home:/usr/src/linux-source-4.4.0/linux-source-4.4.0$make menuconfig
HOSTCC scripts/kconfig/lxdialog/checklist.o
In file included from scripts/kconfig/lxdialog/checklist.c:24:0:
scripts/kconfig/lxdialog/dialog.h:31:20: fatal error: curses.h: 没有那个文件或目录
compilation terminated.
make[2]: *** [scripts/kconfig/lxdialog/checklist.o] 错误 1
make[1]: *** [menuconfig] 错误 2
make: *** [menuconfig] 错误 2
尝试直接执行make config在字符界面进行配置是可以运行的,所以源代码应该十没有错误的,从报错信息可以看出,应该是缺少curses库的支持,这个库的功能是提供字符终端处理库,包括面板和菜单等,这也解释了为什么menuconfig不能执行而config可以执行。
安装curses库:sudo apt-get install libncurses5-dev
再次执行make menuconfig,然后……又报错了……
scripts/sign-file.c:23:30: fatal error: openssl/opensslv.h: 没有那个文件或目录
有了上边的经验,直接开始安装openssl库:sudo apt-get install libssl-dev
然后再再次执行make menuconfig,终于出现了图形化配置界面
根据需求或者一路确定完成配置,可以在/usr/src/linux-source-4.4.0/linux-source-4.4.0目录下生成了config文件(隐藏文件)
然后执行make命令(可以根据不同需求,使用不同参数,生成不同的内核文件),开始漫长的等待……