如何在 Makefile 中定义规则以仅编译已修改的 *.cpp 文件(及其依赖项),而不是所有 *.cpp 文件
假设我有文件:
库:
- one.cpp、one.h
- 二.cpp、二.h
- 三.cpp、三.h
程序:
- program.cpp
有没有办法创建 Makefile,它只编译 *.cpp较上次编译进行了修改吗?
目前我有类似的事情:
SRCS = one.cpp two.cpp three.cpp
OBJS = $(SRCS:.cpp=.o)
all: $(OBJS) program
.cpp.o:
g++ -Wall -c $<
program:
g++ -Wall $(OBJS) program.cpp -o program
clean:
rm -f $(OBJS) program
我工作得很好,但是当我编译程序然后更改two.cpp或two.h时,我需要先运行“make clean”,因为当我第二次运行“make”时,我得到:
Nothing to be done for 'all'.
我会喜欢以这种方式更改我的 Makefile,它会识别我的更改并重新编译该文件及其依赖项(如果 one.cpp 使用经过修改的 Two.cpp 中的代码,则应重新编译两个文件)。
因此,如果我修改 Two.cpp,make 应该执行以下操作:
g++ -Wall -c two.cpp
g++ -Wall $(OBJS) program.cpp -o program
但如果 one.cpp 使用经过修改的 Two.cpp 中的代码,则 make 应该执行以下操作:
g++ -Wall -c one.cpp
g++ -Wall -c two.cpp
g++ -Wall $(OBJS) program.cpp -o program
Lets say I have files:
Libs:
- one.cpp, one.h
- two.cpp, two.h
- three.cpp, three.h
Program:
- program.cpp
Is there way, to create Makefile which will compile only that *.cpp which were modified from last compilation?
Currently I have something like that:
SRCS = one.cpp two.cpp three.cpp
OBJS = $(SRCS:.cpp=.o)
all: $(OBJS) program
.cpp.o:
g++ -Wall -c lt;
program:
g++ -Wall $(OBJS) program.cpp -o program
clean:
rm -f $(OBJS) program
I works fine, but when I compile my program and then change two.cpp or two.h I need to run "make clean" first, because when I secondly run "make" I get:
Nothing to be done for 'all'.
I would like to change my Makefile in that way, it would recognize my changes and recompile that file and its dependencies (if one.cpp uses code from two.cpp which was modified, both files should be recompiled).
So if I modify two.cpp, make should do:
g++ -Wall -c two.cpp
g++ -Wall $(OBJS) program.cpp -o program
But if one.cpp uses code from two.cpp which was modified, make shold do:
g++ -Wall -c one.cpp
g++ -Wall -c two.cpp
g++ -Wall $(OBJS) program.cpp -o program
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,我们创建可执行文件的目标文件先决条件。完成此操作后,只要 SRCS 之一发生更改,Make 就会重建
program
,因此我们不需要OBJS
作为显式目标:然后我们将头文件作为先决条件对象,这样如果我们更改 Three.h,Make 将重建 Three.o:
最后,由于 one.cpp 通过 Two.h 使用来自 Two.cpp 的代码(我希望如此),因此我们将 Two.ha 设为 1 的先决条件.o:
为了让事情更干净、更容易维护,我们使用自动变量:
把它们放在一起,我们得到:
我们还可以做一些事情(例如将program.o添加到
OBJS
),但这对于今天来说已经足够了。First we make the object files prerequisites of the executable. Once this is done, Make will rebuild
program
whenever one of the SRCS changes, so we don't needOBJS
as an explicit target:Then we make the header files prerequisites of the objects, so that if we change three.h, Make will rebuild three.o:
And finally since one.cpp uses code from two.cpp by means of two.h (I hope), we make two.h a prerequisite of one.o:
And to make things cleaner and easier to maintain we use automatic variables:
Put it all together and we get:
There are a few more things we could do (like adding program.o to
OBJS
), but this is enough for today.将命令运行所依赖的文件添加到目标名称的右侧。
例子:
Add the files a command depends upon to run to the right of the target name.
Example:
您需要做的就是告诉
make
.o 文件依赖于 .cpp 文件:All you need to do is tell
make
that the .o file depends on the .cpp file: