hello驱动与Makefile
来源:互联网 发布:中国十大网络神兽动图 编辑:程序博客网 时间:2024/05/18 01:01
#include <linux/init.h>#include <linux/module.h>MODULE_LICENSE("Dual BSD/GPL");MODULE_AUTHOR("xxx");static int __init hello_init(void){ printk(KERN_ALERT "Hello world\n"); return 0;}static void __exit hello_exit(void){ printk(KERN_ALERT " Hello world exit\n");}module_init(hello_init);module_exit(hello_exit);
对应的Makefile可以写成
obj-m = hello.o KERNELDIR ?=/home/lei/linux-2.6.32.2modules: $(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules clean: rm -rf *.o *.ko *.mod.cobi-m指的是编译的目标是编译为模块,模块名为hello.o(如果是obj-y是指编译进内核,obj-n表示不编译)
还可以添加xxx-objs=file1.o file2.o file3.o表明hello.o由这3个文件连接生成
-C $(KERNEL)是切换到指定内核目录读取那里的Makefile
M=$(shell pwd)返回到当前目录,执行当前的Makefile
以下部分转自http://blog.chinaunix.net/uid-27189249-id-3936264.html
拿到内核,首先进行编译选项配置,make menuconfig,对内核编译选项作配置,最后保存的结果会存放在源码目录下的.config文件下。对应的模块的值被赋值成Y,N,M
Y表示要编译进内核,N表示不编译进内核也不编译成模块,M将代码编译成模块通过insmod插入使用。这个配置界面是专门有个系统维护的,如果我们要把自己的东西添加到编译选项中去,得使用Kconfig文件。
建立Kconfig
menuconfig SHREK6410_DRV 在配置界面里的名称,也就是一个大的集合,包括下面好多选项
bool "Drivers for shrek6410 board" 右面的英文为对于这个选项的注释,bool 表示这个选项的类型,只能选择TRUE/FALSE
help
Test driver for s3c6410
if SHREK6410_DRV 如果上面的menuconfig SHREK6410_DRV 选择了,则会出现一下的选项,如果上面的没有选择则一下的选项则不显示
config CHAR01 SHREK6410_DRV 这个大选项下面的第一个小功能模块,tristate表示有三种选择,Y,N,M
tristate "char driver for single file"
help
char01
endif
对应模块的配置选项会保存在.config中如下所示
CONFIG_MY_6410_DRV=y
CONFIG_CHAR01=y
Kconfig需要被添加到上一层的Kconfig以便于被找到。但是2.6.28内核需要将Kconfig目录添加到arch/arm/Kconfig中去,
source "drivers/arm-drv/Kconfig"
然后编写Makefile
obj-$(CONFIG_CHAR01) += mem_test01.o
CONFIG_CHAR01 随着配置文件的配置而改变,当makefile的时候会根据.config将CONFIG_CHAR01对应的值替换到Makefile中(Y,N,M),其实是obj-y,obj-n,obj-m这三个变量,然后根据配置文件就将模块添加到对应的变量上,内核Makefile对三个变量实行不同的编译方法,这就是内核Makefile和Kconfig联合使用的原理。
了解了这么些内容之后可以对内核进行配置和编译了:
Makefiel中 := ?==之间的区别,:=会把文件中所有的定义一下都被替换,=则不是一下全部替换完,而是用一个替换一个,
?=则表示,如果在之前没有定过的则现在定义,如果之前定义过的那就不定义了
如果有些模块不想和内核一起编译,当然也可以利用内核的Makefile为你的代码进行单独的定制,Makefile编写如下:
obj-m := led_test.o
KERNEL := /lib/modules/`uname -r`/build //填写内核Makefile所在的目录
all:
make -C $(KERNEL) M=`pwd` modules //-C 表示调用KERNEL目录下的Makefile
install:
make -C $(KERNEL) M=`pwd` modules_install
depond -A //检测模块建的以来关系
clean:
make -C $(KERNEL) M=`pwd` clean
以下是Kconfig文件和Makefile文件
## Kconfig for shrek6410 board#menuconfig SHREK6410_DRVbool "Drivers for shrek6410 board"help Test driver for s3c6410if SHREK6410_DRVconfig CHAR01tristate "Char driver for single buffer"help xxxconfig CHAR02tristate "Char driver for multi buffer"help xxxconfig LED01tristate "Char driver for led0~3"help Use on | off control ledconfig LED02tristate "Char driver 2 for led0~3"help add led flushconfig KEY01tristate "Test for key interrupt"help Use key2~5 control ledconfig ATOM01tristate "Test for atomic_t"help xxxconfig SPIN01tristate "Test for spinlock_t"help xxxendif #SHREK6410_DRV## Makefile for shrek6410 board# Author: zht# Date: 2013-09-23#obj-$(CONFIG_CHAR01) += mem_test01.oobj-$(CONFIG_CHAR02) += mem_test03.oobj-$(CONFIG_LED01) += led_test02.oobj-$(CONFIG_LED02) += led_test04.oobj-$(CONFIG_KEY01) += key_test02.oobj-$(CONFIG_ATOM01) += atomic_test01.oobj-$(CONFIG_SPIN01) += spin_test01.o
- hello驱动与Makefile
- 最简单的驱动hello.c与Makefile模板
- 最简单的驱动hello.c与Makefile模板
- 最简单的驱动hello.c与Makefile模板
- linux 驱动模块hello.c的Makefile源码
- Hello Makefile
- hello驱动
- LDD3源码分析之hello.c与Makefile模板
- LDD3源码分析之hello.c与Makefile模板
- LDD3源码分析之hello.c与Makefile模板
- LDD3源码分析之hello.c与Makefile模板
- LDD3源码分析之hello.c与Makefile模板
- 最简单的hello模块与Makefile模板
- LDD3源码分析之hello.c与Makefile模板
- LDD3源码分析之hello.c与Makefile模板
- 《Linux驱动》最简单的驱动编写与makefile
- 驱动Makefile
- 驱动Makefile
- 作为一个JAVA程序员,我经常去的地方
- ubuntu下安装Apache+PHP+Mysql
- 配置SVN服务器
- 尝试 Android Scripting Environment
- ACM题目中输入数据的处理(C++版)
- hello驱动与Makefile
- emulator-arm.exe错误
- 杂锦2
- 如何在Activity跳转时不新实例化Action
- LINK : error LNK2001 无法解析的外部符号_mainCRTStartup
- Audio Player
- extern与static分析
- 软件开发-理论性知识(二)
- UIPickerView