多目录多源文件的驱动Makefile模板

很多人知道我搞嵌入式,都说我很有前途,对此我表示感谢,希望自己真的会有个好前途。虽然现在还不能说“四举无成 十年不调”,但一直无所作为,惭愧得很。

我总徘徊在驱动的门外,迟迟不能掌握驱动的编写。一来没有个集中的时间学驱动——自毕业后,已经变得很懒了;二来现实也不允许我一直搞驱动。但是我一直努力将所学的各种知识联系在一起,以提高自己的水平。

 

建立一些属于自己的模板是一件很有必要的事情。无论是代码模块还是其它的东西。以前搞单片机时就意识到了写程序要分模块,要注意代码的重复利用。不过我总是对很多东西很好奇,比如,简单的一个驱动程序Makefile,就搞了好几个版本。从书上得到的简单例子,慢慢扩展适合自己使用,再到在shell中显示提示字符的颜色(比如出错时显示红色提示信息)。

我搞过很多东西,如auto tools、binutils,甚至于GNU编码规范、C99标准,正是这些看似不务正业的东西,花了我大量时间来实践、学习、掌握。不过其中的乐趣及收获,非亲身经历者不能体会也。

 

闲话少说,直奔主题。

本次的驱动Makefile是在以前基础上修改而成的,合适于多个驱动源代码,头文件与实现文件可放到不同目录。

本次工程目录如下:

$ tree
.
|-- Makefile
|-- come.c
|-- configs
|   |-- come.h
|   `-- on.h
`-- on.c

1 directory, 5 files

 

其中come.c和on.c分别为两个源代码文件,内容很简单,就是将hello world程序分开,前者为init,后者为exit。configs目录存放两个自定义的头文件,当然,这里没有什么实际意义的东西。

come.c文件如下:

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

#include <come.h>
static int __init hello_init(void)
{
    printk(KERN_WARNING "Hello world!/n");
    return 0;
}

module_init(hello_init);

 

 on.c文件如下:

#include <linux/module.h>
#include <linux/init.h>
#include <on.h>
static void __exit hello_exit(void)
{
    printk(KERN_ALERT "Goodbye world!/n");
}

module_exit(hello_exit);

MODULE_LICENSE("GPL");

 

 

如果不指定头文件所在位置,编译出错,如下:

                        Compiling ...
make[1]: Entering directory `/usr/src/kernels/2.6.27.5-117.fc10.i686'
  CC [M]  /home/latelee/driver-test/hello-multi/come.o
/home/latelee/driver-test/hello-multi/come.c:4:18: error: come.h: No such file or directory
make[2]: *** [/home/latelee/driver-test/hello-multi/come.o] Error 1
make[1]: *** [_module_/home/latelee/driver-test/hello-multi] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.27.5-117.fc10.i686'
                        [Oops!Error occurred]
make: *** [all] Error 1


后来想借鉴于应用程序的Makefile指定头文件的示例,在Makefile中添加:

INCDIR = ./configs
EXTRA_CFLAGS += $(DEBFLAGS)
EXTRA_CFLAGS += -I$(INCDIR)

还是不行。

今天再次看LDD3的例子,里面的Makefile有这么一句:

modules:
 $(MAKE) -C $(KERNELDIR) M=$(PWD) LDDINC=$(PWD)/../include modules

于是在自己的Makefile中添加类似的语句,结果成功了。

[root@latelee hello-multi]# make
                        Compiling ...
make[1]: Entering directory `/usr/src/kernels/2.6.27.5-117.fc10.i686'
  CC [M]  /home/latelee/driver-test/hello-multi/come.o
  CC [M]  /home/latelee/driver-test/hello-multi/on.o
  LD [M]  /home/latelee/driver-test/hello-multi/GotoHell.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/latelee/driver-test/hello-multi/GotoHell.mod.o
  LD [M]  /home/latelee/driver-test/hello-multi/GotoHell.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.27.5-117.fc10.i686'
                        [Job done!]

 

下面是make clean的效果:

[root@latelee hello-multi]# make clean
                        Cleaning up ...
rm -rf *.cmd *.o *.ko *.mod.c *.symvers *.order *.markers /
        .tmp_versions .*.cmd *~ .*.d
                        [Done.]
 

 

这个Makefile也可以应用于交叉编译情况,由KERNELDIR指定内核目录即可,不过,这个内核必须是适合某个平台的,即交叉编译器必须在内核顶层的Makefile中指定(内核移植时,这一步骤似乎是最先进行的)。如这里指定ARM平台的内核,路径为/home/latelee/my2440/linux-2.6.37.3。

下面看看make的过程并查看生成的模块文件属性:

[root@latelee hello-multi]# make
                        Compiling ...
make[1]: Entering directory `/home/latelee/my2440/linux-2.6.37.3'
  CC [M]  /home/latelee/driver-test/hello-multi/come.o
  CC [M]  /home/latelee/driver-test/hello-multi/on.o
  LD [M]  /home/latelee/driver-test/hello-multi/GotoHell.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/latelee/driver-test/hello-multi/GotoHell.mod.o
  LD [M]  /home/latelee/driver-test/hello-multi/GotoHell.ko
make[1]: Leaving directory `/home/latelee/my2440/linux-2.6.37.3'
                        [Job done!]
[root@latelee hello-multi]# file GotoHell.ko
GotoHell.ko: ELF 32-bit LSB relocatable, ARM, version 1 (SYSV), not stripped
[root@latelee hello-multi]# insmod GotoHell.ko
insmod: error inserting 'GotoHell.ko': -1 Invalid module format


提示信息中红色部分表明这个模块已经是ARM平台的模块了。在x86上是不能加载的。

 

下面附上完整的Makefile:

##############################################################################
#                copyleft @ 2010 2011 Late Lee
# file name:Makefile
# A simple Makefile for device driver by Late Lee from www.latelee.org
# based on LDD3 and other guys works.


# note:
#      You need to change your module name & obj file(s),and you may
#      also need to change 'KERNELDIR'.
#      I hope you can see your module(sth like xx.ko) if you are lucky enough.
##############################################################################

# debug or not
#DEBUG = y
ifeq ( $(DEBUG), y)
        DEBFLAGS = -O -g
else
        DEBFLAGS = -O1
endif

EXTRA_CFLAGS +=  $(DEBFLAGS)
EXTRA_CFLAGS += -I $(INCDIR)

########## change your module name here
MODULE = GotoHell

# obj-m => module
# obj-y => kernel
# foo.o -> foo.ko
########## change your obj file(s) here
$(MODULE)-objs := come.o on.o

ifneq ( $(KERNELRELEASE), )
        obj-m :=  $(MODULE).o

else

        KERNELDIR ?= /lib/modules/$(shell uname -r)/build
# KERNELDIR ?= /home/latelee/my2440/linux-2.6.37.3
        PWD := $(shell pwd)

all:
         $(MAKE_BEGIN)
         @echo 
         @if /
         $(MAKE) -C  $(KERNELDIR) M= $(PWD) INCDIR= $(PWD)/configs modules;/
         #$(MAKE) -C $(KERNELDIR) M=$(PWD) modules;/
         then  $(MAKE_DONE);/
         else /
         $(MAKE_ERR);/
        exit 1; /
         fi
endif

clean:
         $(CLEAN_BEGIN)
        rm -rf *.cmd *.o *.ko *.mod.c *.symvers *.order *.markers /
        .tmp_versions .*.cmd *~ .*.d
         $(CLEAN_END)

install:
         @echo -e  "$(COLOR3) Note:"
         @echo -e  "To install or not install,that is a question.$(RESET)"

modules:
         @echo -e  "$(COLOR3)Do not need to do this. Just rnu 'make'. $(RESET)"

modules_install:
         @echo -e  "$(COLOR3)Do not need to do this.$(RESET)"
love:
         @echo -e  "$(COLOR3)To make or not to make, that is a question.$(RESET)"

.PHONY:all clean install love modules modules_install
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
### nothing
OFFSET=/x1b[21G    # 21 col
COLOR1=/x1b[0;32m  # all --> green
COLOR2=/x1b[1;35m  # clean --> pink
COLOR3=/x1b[1;31m  # error --> red
RESET=/x1b[0m

CLEAN_BEGIN= @echo -e  "$(OFFSET)$(COLOR2)Cleaning up ...$(RESET)"
CLEAN_END= @echo -e  "$(OFFSET)$(COLOR2)[Done.]$(RESET)"

MAKE_BEGIN= @echo -ne  "$(OFFSET)$(COLOR1)Compiling ...$(RESET)"
### I do not forget "@", but it DOES NOT need "@"
MAKE_DONE=echo -e  "$(OFFSET)$(COLOR1)[Job done!]$(RESET)"
MAKE_ERR=echo -e  "$(OFFSET)$(COLOR3)[Oops!Error occurred]$(RESET)"
### nothing end here

############# Makefile end here

注:文中显示的黑框及各种颜色,仅仅是想重现一下在shell下面的显示情况。

PS:这个模板实在不好,还不如CSDN的好。哪天换一个。

再PS:网上已经出现了本文的转载版本了。这些版本有一些不影响阅读的小错误,有的把Makefile中的“/”改了“/”——熟悉C语言宏的人应该知道“/”意味着什么,也应该知道如何修改。有的没有附上Makefile——这是作者有意而为之的测试手段。无论是人工转载还是机器转载,都罢了,我不想将一些非本意的错误被强加于我身上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值