U-Boot中Makefile的学习
来源:互联网 发布:最好的java培训学校 编辑:程序博客网 时间:2024/06/05 08:21
这里还是以一个简单的“hello,world”程序为例,来诠释一下我所体会到的Makefile的强大之初。
现在来看一下我对这个简单程序所创建的文件夹和总控Makefile文件:
lanpeng@lanpeng-laptop:~/hello$ ls -l
总用量 24
drwxr-xr-x 2 lanpeng lanpeng 4096 2010-04-17 20:06 bye
-rw-r--r-- 1 lanpeng lanpeng 199 2010-04-17 19:48 config.mk
drwxr-xr-x 2 lanpeng lanpeng 4096 2010-04-17 20:06 hello
drwxr-xr-x 2 lanpeng lanpeng 4096 2010-04-17 19:43 include
drwxr-xr-x 2 lanpeng lanpeng 4096 2010-04-17 20:06 main
-rw-r--r-- 1 lanpeng lanpeng 911 2010-04-17 20:05 Makefile
lanpeng@lanpeng-laptop:~/hello$ vim config.mk
#编译器的定义
COMPILE :=
CC := $(COMPILE)gcc
CFLAGS := -Wall -O2 -g
CFLAGS += -I$(TOPDIR)/include
export COMPILE CC CFLAGS
#编译规则的定义
%.o : %.c
$(CC) $(CFLAGS) -c -o $@ $<
lanpeng@lanpeng-laptop:~/hello$ vim Makefile
TOPDIR := $(shell pwd)
export TOPDIR
#####################################################################################################
include $(TOPDIR)/config.mk
########################################################################################################
OBJS := $(addprefix main/, main.o)
OBJS += $(addprefix hello/, hello.o world.o)
OBJS += $(addprefix bye/, bye.o)
#以上结果为 OBJS = main/main.o hello/hello.o hello/world.o bye/bye.o
##########################################################################################################
ALL := myhello
all: $(ALL)
myhello:$(OBJS)
$(CC) $(CFLAGS) -o $@ $(OBJS)
$(OBJS):
$(MAKE) -C $(dir $@)
#$(dir main/main.o) 结果是main/ 。 即抽取文件的路径部分。这里开始调用各个子目录下的Makefile。
clean:
$(MAKE) -C main/ clean
$(MAKE) -C hello/ clean
$(MAKE) -C bye/ clean
rm -rf myhello
下面来看看各个子目录下的文件:
(1)main目录
lanpeng@lanpeng-laptop:~/hello/main$ ls
main.c Makefile
lanpeng@lanpeng-laptop:~/hello/main$ vim main.c
#include <hello.h>
int main(int argc, char *argv[])
{
hello_init();
world_init();
bye_init();
return 0;
}
lanpeng@lanpeng-laptop:~/hello/main$ vim Makefile
include $(TOPDIR)/config.mk
OBJ := main.o
all: $(OBJ)
clean:
rm -rf *.o
(2)hello目录
lanpeng@lanpeng-laptop:~/hello/hello$ ls
hello.c world.c Makefile
lanpeng@lanpeng-laptop:~/hello/hello$ vim hello.c
#include <hello.h>
void hello_init()
{
printf("hello,");
}
lanpeng@lanpeng-laptop:~/hello/hello$ vim world.c
#include <hello.h>
void world_init()
{
printf("world!\n");
}
lanpeng@lanpeng-laptop:~/hello/hello$ vim Makefile
include $(TOPDIR)/config.mk
OBJ := hello.o
OBJ += world.o
all : $(OBJ)
clean:
rm -fr *.o
(3)bye目录下
lanpeng@lanpeng-laptop:~/hello/bye$ ls
bye.c Makefile
lanpeng@lanpeng-laptop:~/hello/bye$ vim bye.c
#include <hello.h>
void bye_init()
{
printf("bye bye!\n");
}
lanpeng@lanpeng-laptop:~/hello/bye$ vim makefile
include $(TOPDIR)/config.mk
OBJ := bye.o
all: $(OBJ)
clean :
rm -fr *.o
(4)include 目录下:
lanpeng@lanpeng-laptop:~/hello/include$ ls
hello.h
#ifndef HELLO_H
#define HELLO_H
#include <stdio.h>
void hello_init();
void world_init();
void bye_init();
#endif
这样就通过了总控Makefile去控制各个目录下的子Makefile来完成对程序的编译。
lanpeng@lanpeng-laptop:~/hello$ make
make -C main/
make[1]: 正在进入目录 `/home/lanpeng/hello/main'
gcc -Wall -O2 -g -I/home/lanpeng/hello/include -c -o main.o main.c
make[1]:正在离开目录 `/home/lanpeng/hello/main'
make -C hello/
make[1]: 正在进入目录 `/home/lanpeng/hello/hello'
gcc -Wall -O2 -g -I/home/lanpeng/hello/include -c -o hello.o hello.c
gcc -Wall -O2 -g -I/home/lanpeng/hello/include -c -o world.o world.c
make[1]:正在离开目录 `/home/lanpeng/hello/hello'
make -C bye/
make[1]: 正在进入目录 `/home/lanpeng/hello/bye'
gcc -Wall -O2 -g -I/home/lanpeng/hello/include -c -o bye.o bye.c
make[1]:正在离开目录 `/home/lanpeng/hello/bye'
gcc -Wall -O2 -g -I/home/lanpeng/hello/include -o myhello main/main.o hello/hello.o hello/world.o bye/bye.o
lanpeng@lanpeng-laptop:~/hello$ ./myhello
hello,world!
bye bye!
lanpeng@lanpeng-laptop:~/hello$ make clean
make -C main/ clean
make[1]: 正在进入目录 `/home/lanpeng/hello/main'
rm -rf *.o
make[1]:正在离开目录 `/home/lanpeng/hello/main'
make -C hello/ clean
make[1]: 正在进入目录 `/home/lanpeng/hello/hello'
rm -fr *.o
make[1]:正在离开目录 `/home/lanpeng/hello/hello'
make -C bye/ clean
make[1]: 正在进入目录 `/home/lanpeng/hello/bye'
rm -fr *.o
make[1]:正在离开目录 `/home/lanpeng/hello/bye'
rm -rf myhello
Makefile is very good!!
上一篇:linux静态库和动态库分析
下一篇:串口在minicom下使用退格键的问题
- 世界杰出华商协会集合企业ji...
- Oracle数据库学习之rownum...
- Linux学习笔记----initcall...
- 最近学习python的一些感悟...
- 如何寻找高质量的友情链接...
- linux 常见服务端口
- 【ROOTFS搭建】busybox的httpd...
- xmanager 2.0 for linux配置
- 什么是shell
- linux socket的bug??
- 初学UNIX环境高级编程的,关于...
- chinaunix博客什么时候可以设...
- 一个访问量较大网站的服务器。...
- 收音机驱动与v4l2架构的关系,...
- 如何将linux驱动改为裸机驱动(...
- U-Boot中Makefile的学习
- u-boot中makefile的执行步骤
- U-BOOT中Makefile分析
- u-boot的Makefile分析
- U-boot的Makefile分析
- u-boot的Makefile分析
- u-boot的Makefile分析
- u-boot的Makefile分析
- U-Boot的Makefile分析
- u-boot的Makefile分析
- u-boot的Makefile分析
- u-boot的Makefile分析
- u-boot的Makefile分析
- u-boot的Makefile分析
- u-boot的Makefile分析
- u-boot的Makefile分析
- u-boot的Makefile分析
- u-boot的Makefile分析
- iconv的使用方法
- linux内核双向循环链表实例
- ARM汇编实验-工作模式
- ARM9裸机的UART程序测试
- linux静态库和动态库分析
- U-Boot中Makefile的学习
- 串口在minicom下使用退格键的问题
- CPU的规则和体系结构综述
- 利用异或逻辑交换两个数
- ARM-Linux下交叉编译opessl-1.0.0
- ARM-Linux下交叉编译lzo
- mini2440串口的输入和输出整理
- Linux下DNW源代码--mini2440
- linux MTD 源码分析-word文档