Linux学习笔记-增量编译(Makefile进一步使用)

Linux学习笔记-增量编译(Makefile进一步使用)增量编译 在 VC 中就是增量编译 当某个 cpp 更新后 只编译这个 cpp 文件 称为增量编译 在 Linux 中 通过控制 Makefile 实现增量编译的效果 Makefile 中使用 依赖 dependency 来实现增量编译 target dependencies tab system command1 tab

大家好,我是讯享网,很高兴认识大家。

增量编译

在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.o g++ main.o first.o second.o -o helloWorld main.o:main.cpp first.h second.h g++ -c main.cpp -o main.o first.o:first.cpp first.h g++ -c first.cpp -o first.o second.o:second.cpp second.h g++ -c second.cpp -o second.o clean: 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.h     g++ -c second.cpp -o second.o

这个代码执行完后,才执行:

讯享网helloWorld:main.o first.o second.o g++ main.o first.o second.o -o helloWorld

 

小讯
上一篇 2025-03-12 20:14
下一篇 2025-03-30 11:13

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/104369.html