cmake linux模板 多目录_多目录多源文件的驱动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

#include

#include

static int __init hello_init(void)

{

printk(KERN_WARNING "Hello world!\n");

return 0;

}

module_init(hello_init);

on.c文件如下:

#include

#include

#include

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]# makeCompiling ...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:

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值