模块编译hello
来源:互联网 发布:腾讯云和阿里云 知乎 编辑:程序博客网 时间:2024/05/19 07:11
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。
- hello world 模块编译
- 模块编译hello
- hello world 模块编译
- Android编译环境--- 编译hello world模块
- 编译Native C的hello,world模块
- Hello World模块的编译和运行
- 如何编译第一个模块hello
- hello内核模块编译的全过程
- Fedora下编译Hello模块驱动
- 直接编译hello模块进linux内核
- 编译内核模块Hello World V1.0
- 编译内核模块Hello World V2.0
- 编译内核模块Hello World V3.0
- 编译内核模块Hello World V4.0
- Linux 内核模块编译(hello.c)
- hello.c内核模块编译 -- linux内核
- hello模块编译及Makefile解读
- linux设备驱动程序的hello模块编译过程
- 根据手机屏幕的大小动态设置图片宽和高
- 按文件大小列出指定目录下文件(du,sort)
- Volley实战用法
- How-to: enable spark sql in cdh version spark
- linux下rsync的使用
- 模块编译hello
- HDU KiKi's K-Number (树状数组+二分查找)
- Android5.1 + Android Studio ADB网络调试
- 1070. Mooncake (25)
- InputStream、InputStreamReader和Reader的关系
- Android Parcelable和Serializable的区别(二)
- JAVA动态代理的作用
- Android错误之java.lang.RuntimeException: Binary XML file line #160: You must supply a layout_width attr
- 九度oj 1008