Linux学习笔记-增量编译(Makefile进一步使用)
增量編譯
在VC中就是增量編譯!
當某個cpp更新后,只編譯這個cpp文件,稱為增量編譯。
在Linux中,通過控制Makefile實現增量編譯的效果
Makefile中使用“依賴dependency”來實現增量編譯
target:dependencies
<TAB>system command1
<TAB>system command..
依賴是一個文件列表,當有文件更新時,執行這條規則
注意:根據文件的修改時間來判斷是否要更新,
比如某個依賴文件的時間比target的時間要新就編譯
特例:
時間比較:
target(T1):dependencies(T2)
1.若target文件不存在,則T1為0
2.若dependencies為空,則T2為0
Makefile比較T2與T1
if(T1==0)執行
else if(T2>T1)執行
else "已是最新,不執行規則"
?
舉個栗子:
如下面的這個代碼:
first.h
void firstPrint();first.cpp
#include "first.h" #include <stdio.h>void firstPrint() {printf("firstPrint called!\n"); }second.h
void secondPrint();second.cpp
#include "second.h" #include <stdio.h>void secondPrint() {printf("secondPrint called!\n"); }main.cpp
#include "first.h" #include "second.h"#include <stdio.h>int main() {firstPrint();secondPrint();printf("main called!\n");return 0; }?
此時寫一個Makefile,如下圖:
helloWorld:main.o first.o second.og++ main.o first.o second.o -o helloWorldmain.o:main.cpp first.h second.hg++ -c main.cpp -o main.ofirst.o:first.cpp first.hg++ -c first.cpp -o first.osecond.o:second.cpp second.hg++ -c second.cpp -o second.oclean:rm -rf *.o就是因為這樣,就可以采用增量編譯了!
?
如下,當第一次調用Makefile生成文件時:
第二次調用時候,他會提示,已經是最新的,如下圖:
當修改了second.cpp中的文件后,注意看現象:
程序首先執行Makefile中的helloWorld:main.o first.o second.o,
編譯helloWorld時,他先回去找main.o發現main.o沒有啥改變就不管啦,他又去找first.o發現時間戳正常,也不去管了,找到second.o時候,發現最新的文件比他second的文件新,就執行:
second.o:second.cpp second.hg++ -c second.cpp -o second.o這個代碼執行完后,才執行:
helloWorld:main.o first.o second.og++ main.o first.o second.o -o helloWorld?
新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!總結
以上是生活随笔為你收集整理的Linux学习笔记-增量编译(Makefile进一步使用)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ opengl 矩阵的压栈与出栈
- 下一篇: Linux学习笔记-管道的读写特性