Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2242067
  • 博文数量: 395
  • 博客积分: 10994
  • 博客等级: 上将
  • 技术积分: 5586
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-17 19:49
文章存档

2014年(1)

2013年(10)

2012年(74)

2011年(303)

2010年(7)

分类: LINUX

2011-03-22 17:11:40

    如果大家编译过内核的话,你会发现linux源码中有很多的makefile,每个目录下都有一个makefiel,当你在根目录下执行make命令的时候,
其实是执行根目录下的makfile,然后再根目录下的makefile再执行各个目录下的makefile文件,这是到底怎么实现呢?

   其实这就是makefile的嵌套执行,下面是我学习的时候总结的
1。我在/home中建立了一个目录

mkdir hello_tmp

2.cd hello_tmp

3.建立c语言源文件a.c
vim a.c
内容如下:
#include
int main()
{

        hello();
        return 0;
}

4. 在此目录中建立文件
mkdir home

5。cd home
6。再次建立一个c源文件

vim b.c
内容如下:
#include
void hello()
{
        printf("hello\n");
}

7。然后在这个目录中创建Makefile,
vim Makefile
内容如下
b.o:b.c
        cc -c -o b.o b.c
8。回到上层目录
cd ..
9。编辑上层Makefile
内容如下
VPATH=./home
obj=a.o b.o

.PHONY: all
all: test $(obj)

test:$(obj)
        cc -o test a.o ./home/b.o

b.o:b.c
        cd ./home && make

.PHONY:clean
clean:
        rm -rf *.o test
        rm ./home/*.o


这其中的内容你可以自己好好想一想,相当的经典,这样的话,就能完成顶层makefile调用底层makefile了,
VPATH是一个特殊变量,Make在当前路径中找不到源文件的情况下会自动到VPATH指定的路径中去搜索寻找。VPATH的使用方法为:
VPATH:目录 :目录.....

10.然后你就可以在顶层目录中使用
make
我的输出为:
cc    -c -o a.o a.c
cd ./home && make
make[1]: Entering directory `/home/hello_temp/home'
cc -c -o b.o b.c
make[1]: Leaving directory `/home/hello_temp/home'
cc -o test a.o ./home/b.o
在编译内核的时候是不是经常出现make[1]呀

然后:
ls
我的下面是:
a.c  b.bbb  log   mach-mini2440.c  Makefile   test
a.o  home   log1  mach-smdk2440.c  Makefile1
(上面有一些我自己见的一些乱的文件,你可以看到出现了test文件了,可执行)
ls -l
-rw-r--r-- 1 root root    57 03-18 11:47 a.c
-rw-r--r-- 1 root root   776 03-18 16:40 a.o
-rw-r--r-- 1 root root    13 03-18 11:47 b.bbb
drwxr-xr-x 2 root root  4096 03-18 16:40 home
-rw-r--r-- 1 root root 18997 03-18 11:47 log
-rw-r--r-- 1 root root    43 03-18 11:47 log1
-rw-r--r-- 1 root root  6020 03-18 11:47 mach-mini2440.c
-rw-r--r-- 1 root root  4331 03-18 11:47 mach-smdk2440.c
-rw-r--r-- 1 root root   177 03-18 16:23 Makefile
-rw-r--r-- 1 root root   246 03-18 11:58 Makefile1
-rwxr-xr-x 1 root root  5035 03-18 16:40 test


11。 执行./test
[root@bogon hello_temp]# ./test
hello

是不是听有意思的,,这就是makefile的强大之处
。。。。。。。
阅读(5756) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~