粉丝留言,想知道如何使用Makefile给多个文件和多级目录建立一个工程,必须安排!
关于Makefile的入门参考文章,可以先看这篇文章:
为了让大家有个更加直观的感受,一口君将之前写的一个小项目,本篇在该项目基础上进行修改。
该项目详细设计和代码,见下文:
《从0写一个《电话号码管理系统》的C入门项目【适合初学者】》
好了,开始吧!
我们将该项目的所有功能函数放到以该函数名命名的c文件,同时放到对应名称的子目录中。
比如函数allfree(),存放到 allfree/allfree.c中
最终目录结构如下图所示:
peng@ubuntu:/mnt/hgfs/code/phone$ tree .
.
├── allfree
│ ├── allfree.c
│ └── Makefile
├── create
│ ├── create.c
│ └── Makefile
├── delete
│ ├── delete.c
│ └── Makefile
├── display
│ ├── display.c
│ └── Makefile
├── include
│ ├── Makefile
│ └── phone.h
├── init
│ ├── init.c
│ └── Makefile
├── login
│ ├── login.c
│ └── Makefile
├── main
│ ├── main.c
│ └── Makefile
├── Makefile
├── menu
│ ├── Makefile
│ └── menu.c
├── scripts
│ └── Makefile
└── search
├── Makefile
└── search.c
11 directories, 22 files
直接看下编译结果吧:
peng@ubuntu:/mnt/hgfs/code/phone$ make
make[1]: Entering directory '/mnt/hgfs/code/phone/allfree'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/mnt/hgfs/code/phone/allfree'
make[1]: Entering directory '/mnt/hgfs/code/phone/create'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/mnt/hgfs/code/phone/create'
make[1]: Entering directory '/mnt/hgfs/code/phone/delete'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/mnt/hgfs/code/phone/delete'
make[1]: Entering directory '/mnt/hgfs/code/phone/display'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/mnt/hgfs/code/phone/display'
make[1]: Entering directory '/mnt/hgfs/code/phone/init'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/mnt/hgfs/code/phone/init'
make[1]: Entering directory '/mnt/hgfs/code/phone/login'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/mnt/hgfs/code/phone/login'
make[1]: Entering directory '/mnt/hgfs/code/phone/menu'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/mnt/hgfs/code/phone/menu'
make[1]: Entering directory '/mnt/hgfs/code/phone/search'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/mnt/hgfs/code/phone/search'
make[1]: Entering directory '/mnt/hgfs/code/phone/main'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/mnt/hgfs/code/phone/main'
gcc -Wall -O3 -o phone allfree/*.o create/*.o delete/*.o display/*.o init/*.o login/*.o menu/*.o search/*.o main/*.o -lpthread
phone make done!
运行结果如下: