makefile 的规则
假设我有一些用于 Unix 的 Makefile
prog:a.o b.o c.o
gcc a.o b.o c.o -o prog
a.o:a.c a.h b.h
gcc -c a.c
b.o:b.c b.h
gcc -c b.c
c.o:c.c c.h b.h
gcc -c c.c
我读到,如果我更改 *.c 文件,我必须仅重新编译该文件,但如果我更改 *.h 文件,我必须重新编译所有依赖于我的 *.h 文件的文件,在这两个文件之后无论如何我必须从头开始链接
所有文件
1)
让我们假设我只更改bh,那么我需要做什么? 我的变体是 make ao bo co
我说得对吗?
2)
假设我只更改 cc,那么我需要做什么? 我的变体是 make co
我说得对吗?
最后,如果我写 make co
例如我的 makefile 仅重新编译 co
但链接器怎么样
?我只有 gcc -c cc
而没有 -o
提前感谢您的帮助
let's assume I have some Makefile for Unix
prog:a.o b.o c.o
gcc a.o b.o c.o -o prog
a.o:a.c a.h b.h
gcc -c a.c
b.o:b.c b.h
gcc -c b.c
c.o:c.c c.h b.h
gcc -c c.c
I read that if I change *.c file I must recompile only this file, but if I change *.h file I must recompile all files which are depending on my *.h file, after both cases anyway I must link
all files from the beginning
1)
let's assume that I change only b.h, so what do I need to do?
my variant is make a.o b.o c.o
am I right?
2)
let's assume that I change only c.c, so what do I need to do?
my variant is make c.o
am I right?
and finally if I write make c.o
for example my makefile ONLY recompile c.o
but what about linker
? I have only gcc -c c.c
and not -o
thanks in advance for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所说的“我需要做什么?”到底是什么意思?在每种情况下,只需运行 make prog,然后让 make 找出答案,这就是重点。
编辑:如果您好奇 make 在每种情况下会做什么,假设您每次都运行
make prog
,请继续阅读:在情况 1) bh 已更改,因此任何依赖于它的内容 (ao 、 bo 和 co) 将被重建,并且 prog 将被重新链接。
在情况 2) co 只是 cc 的直接依赖项,因此仅构建 co 并重新链接 prog。
顺便说一句,gcc 只需在项目目录中运行
gcc -MM *.c
即可为您提供依赖项。What exactly do you mean 'what do I need to do?'. In every case, just run
make prog
, and let make figure it out, that's the whole point.EDIT: If you're curious what make will do in each case, assuming you're running
make prog
every time, read on:In case 1) b.h has changed, so anything depending on it (a.o, b.o and c.o) will be rebuilt, and prog will be relinked.
In case 2) c.o is only a direct dependency of c.c, so only c.o is built and prog relinked.
BTW, gcc can give you it's take on your dependencies by just running
gcc -MM *.c
in your project directory.在所有情况下,您只需执行
或仅
执行 Makefile 的想法是
make
可以检查依赖项,找出过时的内容,并仅执行所需的重新编译(仅此而已)。如果您更改
.c
文件,显然它将重新编译该.c
文件,并且因为这会更新.o
文件,其中 < code>prog 取决于,它也会重新链接prog
。如果您更改
.h
文件,这将触发一个或多个.c
文件的重新编译,这也会再次导致prog
重新链接。In all cases, you just do
or just
The idea of having the Makefile is that
make
can examine the dependencies, find out what's out of date, and do just the required recompilation (and no more).If you change a
.c
file, obviously it's going to recompile that.c
file, and because this updates a.o
file on whichprog
depends, it will also relinkprog
.If you change a
.h
file, this will trigger the recompilation of one or more.c
files, which again will also causeprog
to be relinked.