快速编写“专家级”makefile(4.打造更专业的编译环境——增强可使用性)

    从前面看来,为了编译 huge 项目需要进入不同的目录运行 make ,我们可以简化它:
    huge / build / Makefile
  
  
  1. .PHONY : all clean
  2. ROOT = $(realpath ..)
  3. DIRS = $(ROOT)/code/foo/src\
  4. $(ROOT)/code/bar/src\
  5. $(ROOT)/code/huge/src
  6. RM = rm
  7. RMFLAG = -rf
  8. RMS = $(ROOT)/build/exes $(ROOT)/build/libs
  9. all clean:
  10. @set -e;\
  11. for dir in $(DIRS);\
  12. do \
  13. cd $$dir && $(MAKE) ROOT=$(ROOT) %@;\
  14. done
  15. @set -e;\
  16. if["$(MAKECMDGOALS)" = "clean"]; then $(RM) $(RMFLAG) $(RMS); fi
  17. @echo ""
  18. @echo ":-) Completed"
  19. @echo ""
    在这个 Makefile 中,使用 realpath 函数以获得项目的根目录,并将这个值传递给每一个 DIRS 变量中所列目录中的 Makefile 。这样就可以在 build 编译整个项目了,并不需要在 Shell 上导出 ROOT 环境变量。
    其中还使用了 Shell 中的 for 语句,用于遍历 DIRS 变量中的每一个目录,以便在各目录中运行 make 命令。
    运行结果:
/Makefile/huge/build
$ make
make[1] : Entering directory  /Makefile/huge/code/foo/src
/Makefile/huge/build/make.rule : 43 : deps/foo.dep : No such file or directory
mkdir deps
Creating deps / foo.dep ...
make[1] : Leaving directory  /Makefile/huge/code/foo/src
make[1] : Entering directory  /Makefile/huge/code/foo/src
mkdir /Makefile/huge/build/libs
mkdir objs
gcc -I/Makefile/huge/code/foo/inc  -o  objs/foo.o  -c  foo.c
ar crs /Makefile/huge/build/libs/libfoo.a  objs/foo.o
make[1] : Leaving directory  /Makefile/huge/code/foo/src
make[1] : Entering directory  /Makefile/huge/code/bar/src
/Makefile/huge/build/make.rule : 43 : deps/bar.dep : No such file or directory
mkdir deps
Creating deps / bar.dep ...
make[1] : Leaving directory  /Makefile/huge/code/bar/src
make[1] : Entering directory  /Makefile/huge/code/bar/src
mkdir objs
gcc -I/Makefile/huge/code/foo/inc  -o  objs/bar.o  -c  bar.c
ar crs /Makefile/huge/build/libs/libbar.a  objs/bar.o
make[1] : Leaving directory  /Makefile/huge/code/bar/src
make[1] : Entering directory  /Makefile/huge/code/huge/src
/Makefile/huge/build/make.rule : 43 : deps/main.dep : No such file or directory
mkdir deps
Creating deps / main.dep ...
make[1] : Leaving directory  /Makefile/huge/code/huge /src
make[1] : Entering directory  /Makefile/huge/code/ huge /src
mkdir objs
gcc -I/Makefile/huge/code/foo/inc  -I/Makefile/huge/code/bar/inc -o  objs/main.o  -c  main.c
gcc -L/Makefile/huge/build/libs -o /Makefile/huge/build/exes/huge.exe  objs/main.o  -lfoo -lbar
make[1] : Leaving directory  /Makefile/huge/code/huge /src
:-) Completed
/Makefile/huge/build
$ make clean
make[1] : Entering directory  /Makefile/huge/code/foo/src
rm -rf objs deps /Makefile/huge/build/libs/libfoo.a
make[1] : Leaving directory  /Makefile/huge/code/foo/src
make[1] : Entering directory  /Makefile/huge/code/bar/src
rm -rf objs deps /Makefile/huge/build/libs/libbar.a
make[1] : Leaving directory  /Makefile/huge/code/bar/src
make[1] : Entering directory  /Makefile/huge/code/huge/src
rm -rf objs deps /Makefile/huge/build/exes/huge.exe
make[1] : Leaving directory  /Makefile/huge/code/huge/src
rm -rf  /Makefile/huge/build/exes   /Makefile/huge/build/libs
:-) Completed
    只要看见笑脸,就表示项目编译成功!
            参考文献:《专业嵌入式软件开发》李云·著                                                             2016年7月6日,星期三
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值