Linux内核编译

                                           内核编译

环境配置:

OS-x86-Centos6.3 、Kernel-2.6.32 、gcc-4.4.7、编译内核版本3.13.2。

yum install ncurses-devel

注意:编译内核2.6x以下的尽量选择gcc版本<=4.6,否则,编译易出错!

获取内核版本:

#wget https://mirrors.edge.kernel.org/pub/linux/kernel/v3.x/linux-3.13.2.tar.gz

 #tar -zxvf linux-3.13.2.tar.gz -C /usr/src/kernels/

编译:

1--配置内核 .config 文件

           #cp /boot/config-2.6.32-279.el6.i686 .config  :新手举荐,直接使用2.6.32的config

           #make config:遍历选择所要编译的内核特性
           #make allyesconfig:配置所有可编译的内核特性
           #make allnoconfig:并不是所有的都不编译
           #make menuconfig:这种就是打开一个文件窗口选择菜单
           #make kconfig(KDE桌面环境下,并且安装了qt开发环境)
           #make gconfig(Gnome桌面环境,并且安装gtk开发环境)

2--make

           #make -j 4     #-j 指定cpu的核心数,最大可以是实际CPU核心数目的两倍,这样编译速度会加快

3--安装内核模块

           # make modules_install

4--安装内核

           # make install

5--验正并测试

           # cat /boot/grub/grub.conf

6--重启

              选择安装好的内核3.13.2进入系统

7--测试helloworld.ko的安装与卸载

新建一个目录helloworld

// helloworld.c
#include <linux/init.h>
#include <linux/module.h>

MODULE_LICENSE("GPL");
static int hello_init(void)
{
  printk(KERN_ALERT "Hello world
");
  return 0;
}
static void hello_exit(void)
{
  printk(KERN_ALERT "Goodbye
");
}

module_init(hello_init);
module_exit(hello_exit);
#Makefile
obj-m :=helloworld.o
KERNEL :=/usr/src/kernels/linux-3.13.2/
#注意这里的路径要选择你编译过且正在运行的内核版本!
PWD :=$(shell pwd)
modules :
        $(MAKE) -C $(KERNEL) M=$(PWD) modules
.PHONEY:clean
clean :
        rm -f *.o *.ko *.mod.c *.order *.symvers

make编译

安装/卸载helloworld.ko模块

到这里就成功的完成环境配置,接下来开始学习Linux 内核API编程

以上站在巨人的肩膀学习,感谢

https://blog.csdn.net/qq_15437667/article/details/69490325

https://blog.csdn.net/reblue520/article/details/50786092

原文地址:https://www.cnblogs.com/alex-gc/p/11143960.html