makefile中"模式规则"的引入和介绍------%:%.cpp
来源:互联网 发布:java游戏代码大全 编辑:程序博客网 时间:2024/06/18 04:50
原文地址:http://blog.csdn.net/stpeace/article/details/53054679
目录下有test1.cpp, test2.cpp, test3.cpp三个独立文件(彼此之间并无依赖与调用关系), 要编译成三个可执行程序, 怎么搞呢? 我们看看makefile:
- test1: test1.cpp
- test2: test2.cpp
- test3: test3.cpp
- clean:
- rm -f test1 test2 test3
- taoge@localhost Desktop> make
- g++ test1.cpp -o test1
- taoge@localhost Desktop>
那改一下:
- test1 test2 test3: test1.cpp test2.cpp test3.cpp
- clean:
- rm -f test1 test2 test3
- taoge@localhost Desktop> make
- g++ test1.cpp test2.cpp test3.cpp -o test1
- /tmp/ccAX6NNB.o: In function `main':
- test2.cpp:(.text+0x72): multiple definition of `main'
- /tmp/ccaITY1Z.o:test1.cpp:(.text+0x72): first defined here
- /tmp/cc4Wsk9m.o: In function `main':
- test3.cpp:(.text+0x72): multiple definition of `main'
- /tmp/ccaITY1Z.o:test1.cpp:(.text+0x72): first defined here
- collect2: ld returned 1 exit status
- make: *** [test1] Error 1
- taoge@localhost Desktop>
那怎么办? 我们反思一下上面的两次失败:
在前一次中, 我们其实只定义了一个target文件(因cpp独立), 也就是test1.
在后一次中, 我们定义了三个target文件, 可是, 杂糅链接了(依赖关系杂糅)。
那好, 我们来改进一下, 兼顾到上面两种情况:
- all: test1 test2 test3
- test1: test1.cpp
- test2: test2.cpp
- test3: test3.cpp
- clean:
- rm -f test1 test2 test3
- taoge@localhost Desktop> make clean
- rm -f test1 test2 test3
- taoge@localhost Desktop> make all
- g++ test1.cpp -o test1
- g++ test2.cpp -o test2
- g++ test3.cpp -o test3
- taoge@localhost Desktop> ls
- makefile test1 test1.cpp test2 test2.cpp test3 test3.cpp
- taoge@localhost Desktop>
我们思考一下, 为什么这样可以? make命令首先找到all标志, 发现了必须要生成test1, test2, test3, 于是就往下找, 去生成他们, 于是就达到了我们的目标。
这里有个疑问, 为什么没有生成all文件呢? 因为all下面并没有待执行的命令,也无法自动推导。 我们来看看改动的makefile:
- all: test1 test2 test3
- @echo testing
- test1: test1.cpp
- test2: test2.cpp
- test3: test3.cpp
- clean:
- rm -f test1 test2 test3
- taoge@localhost Desktop> make all
- g++ test1.cpp -o test1
- g++ test2.cpp -o test2
- g++ test3.cpp -o test3
- testing
- taoge@localhost Desktop>
以上部分应该比较好动, 现在还有个问题, 如果有100个cpp文件, 那该怎么搞起呢? 写到test100? 麻烦死了, 明显不符合计算机的思维, 好, 那就搞个模式规则吧, 如下:
- all: test1 test2 test3
- %:%.cpp
- g++ $< -o $@
- clean:
- rm -f test1 test2 test3
- taoge@localhost Desktop> make clean
- rm -f test1 test2 test3
- taoge@localhost Desktop> make all
- g++ test1.cpp -o test1
- g++ test2.cpp -o test2
- g++ test3.cpp -o test3
- taoge@localhost Desktop>
- %:%.cpp
- g++ $< -o $@
其实, 如上程序还没有解决根本问题, 继续优化吧:
- CPPLIST = $(wildcard *.cpp) # get cpp file list
- TARGET = $(patsubst %.cpp, %, $(CPPLIST)) # get corresponding target file
- all: $(TARGET)
- @echo ------------------
- @echo log1: $(TARGET)
- @echo log2: $(CPPLIST)
- %:%.cpp
- g++ $< -o $@
- clean:
- rm -f $(TARGET)
- taoge@localhost Desktop> make clean
- rm -f test1 test2 test3
- taoge@localhost Desktop> make all
- g++ test1.cpp -o test1
- g++ test2.cpp -o test2
- g++ test3.cpp -o test3
- ------------------------
- log1: test1 test2 test3
- log2: test1.cpp test2.cpp test3.cpp
- taoge@localhost Desktop>
本文所谓的模式规则, 其实就是:
- %:%.cpp
- g++ $< -o $@
最后想说一下, 有点循环的感觉啊!
阅读全文
0 0
- makefile中"模式规则"的引入和介绍------%:%.cpp
- makefile中"模式规则"的引入和介绍------%:%.cpp
- Makefile的后缀规则和模式规则
- C语言的本质(37)——makefile之隐含规则和模式规则
- 对makefile中:单冒号普通规则和::双冒号规则的理解 -转
- Makefile: Makefile 隐式规则 模式规则
- Makefile中的模式规则
- makefile模式规则
- 研发团队中引入变化的思路和模式
- Eclipse中引入ActionBarSherlock和ActionBarSherlock 介绍
- EasyUI介绍和在mvc 中引入
- makefile中关于all和.PHONY .cpp.o
- makefile中关于all和.PHONY .cpp.o
- makefile中关于all和.PHONY .cpp.o
- MAKEFILE文件的语法规则和配置
- Makefile: 混合的隐含和普通规则
- makefile文件的语法规则和配置
- Makefile:混合的隐含和通用规则
- Struts+displaytag国际化简单使用
- 大前端学习线路
- SQL to Aggregation Mapping Chart
- docker redis 快速启动
- struts2_day03_15_el表达式为什么获取值栈数据
- makefile中"模式规则"的引入和介绍------%:%.cpp
- select操作
- 求js数组的最大值和最小值的四种方法
- oracle 使用自定义函数查询条件效率非常低
- java调用http接口
- maven项目出现missing artifact错误解决办法
- 数据库概述
- Linux文件存储之inode、硬链接、软链接
- HDU 4283 You Are the One【区间DP】