模块编译hello

1:新建文件和编写代码

#mkdir /home/world/helloworld
#cd  /home/world/helloworld
#gedit hello_world.c
输入以下内容保存

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <asm/uaccess.h>

//  初始化Linux驱动
static int hello_world_init(void)
{
//  输出日志信息
 printk("hello_world_init_success\n");
  return 0;
}

// 卸载Linux驱动
static void hello_world_exit(void)
{
 //  输出日志信息
 printk("hello_world_init_exit_success\n");
}

//  注册初始化Linux驱动的函数
module_init( hello_world_in);
//  注册卸载Linux驱动的函数
module_exit( hello_world_exit);

MODULE_AUTHOR("lining");
MODULE_DESCRIPTION("statistics of hell world.");
MODULE_ALIAS("world count module.");
MODULE_LICENSE("GPL");


2;编译

a,在ubuntu下编译
 
  进入目录
 
  #cd  /home/world/helloworld
 
  make 文件
 
  make -C  /usr/src/linux-headers-3.03.15-generic M=/home/world/helloworld
 
  -C后面跟的是linux内核目录。M后面跟的是代码编译目录下
 
  编译后,出现内核模块hello_world.ko
 
  安装模块 #insmod  hello_world.ko
 
  查看模块是否安装#lsmode | grep hello_world
 
  卸载模块 #rmmod hello_world
 
  查看日志 #dmesg | grep hello_world | tail -n 2
 
  或者cat  /var/log/syslog | grep hello_world | tail -n 2
 
  查看模块信息:#modinfo hello_world.ko
 
  查看设备 ls -a /dev
 
  查看主次设备号 ls -l /dev
 
  查看设备 是否在设备文件中
 
  #cat /proc/devices

b,在ARM下手动编译

需要建立交叉编译链

需要编译成功编译内核,才可以开始编译自己要添加进去的驱动


内核和模块是分开编译的

模块和内核不在一起的编译, 就是在现有的内核中追加一些内核模块时, 不需要将内核也重新编译.
 cd$ lichee/linux-3.4/    #进入到kernel 目录 
 lichee/linux-3.4$ cp arch/arm/configs/cubietruck_config  .config           
 lichee/linux-3.4$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- kernel_defconfig #是编译内核源码
#用4线程是创建 uImage 格式的内核映像以及创建内核模块

 lichee/linux-3.4$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j4 uImage  
 make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j4 uImage
 #cd  /home/world/helloworld 
 #make  ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-  -C  /home/work/Cubieboard2/lichee/linux-3.4 M=/home/world/helloworld

在Linux下,用Make 命令来编译程序,
CROSS_COMPILE给出了编译程序所用的交叉工具链的名称,比如:
CROSS_COMPILE=arm-linux-gnueabi- 表示交叉编译器的gcc为
arm-linux-gnueabi-
ARCH给出了目标处理器的架构
这里用的是arm 处理器,
通过这个命令,把这些参数传递给Makefile中的预留参数,程序就可以顺利编译下去了。

C,在ARM下编译用make编译原来Makefile,和kconfig两文件

1:在helloworld目录中新增Kconfig和Makefile两个文件,其中Kconfig是在编译前执行配置命令make menuconfig时用到的,而Makefile是执行编译命令make是用到的:
       Kconfig文件的内容

       config HELLO
           tristate "First Android Driver"
           default n
           help
           This is the first android driver.

      Makefile文件的内容
      obj-$(CONFIG_HELLO) += hello_world.o

      在Kconfig文件中,tristate表示编译选项HELLO支持在编译内核时,hello模块支持以模块、内建和不编译三种编译方法,默认是不编译,因此,在编译内核前,我们还需要执行make menuconfig命令来配置编译选项,使得hello可以以模块或者内建的方法进行编译。
      在Makefile文件中,根据选项HELLO的值,执行不同的编译方法。

2. 修改arch/arm/Kconfig和drivers/kconfig两个文件,在menu "Device Drivers"和endmenu之间添加一行:
      source "drivers/helloworld/Kconfig"

        这样,执行make menuconfig时,就可以配置helloworld模块的编译选项了。.
 
3. 修改drivers/Makefile文件,添加一行:
        obj-$(CONFIG_HELLO) += helloworld/

4. 配置编译选项:
        lichee/linux-3.4$  make menuconfig
        找到"Device Drivers" => "First Android Drivers"选项,设置为y。
        注意,如果内核不支持动态加载模块,这里不能选择m,虽然我们在Kconfig文件中配置了HELLO选项为tristate。要支持动态加载模块选项,必须要在配置菜单中选择Enable loadable module support选项;在支持动态卸载模块选项,必须要在Enable loadable module support菜单项中,选择Module unloading选项。


5. 编译:
        lichee/linux-3.4$  make
        编译成功后,就可以在hello目录下看到hello_world .o文件了,这时候编译出来的zImage已经包含了hello驱动。

6,运行新编译的内核文件,验证hello驱动程序是否已经正常安装:

Linux Kernel镜像zImage和Android镜像文件system.img、userdata.img和ramdisk.img。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值