Makefile 简明教程
Makefile - Recompilation
make 程序是一个智能实用程序,它会根据您对源文件所做的更改进行工作。如果您有四个文件,main.cpp、hello.cpp、factorial.cpp 和 functions.h,那么所有剩余文件都依赖于 functions.h,main.cpp 依赖于 hello.cpp 和 factorial.cpp。因此,如果您对 functions.h 做出任何更改, make 会重新编译所有源文件以生成新的对象文件。但是,如果您对 main.cpp 做出任何更改,由于这不依赖于任何其他文件,所以只重新编译 main.cpp 文件,而不会重新编译 help.cpp 和 factorial.cpp。
The make program is an intelligent utility and works based on the changes you do in your source files. If you have four files main.cpp, hello.cpp, factorial.cpp and functions.h, then all the remaining files are dependent on functions.h, and main.cpp is dependent on both hello.cpp and factorial.cpp. Hence if you make any changes in functions.h, then the make recompiles all the source files to generate new object files. However if you make any change in main.cpp, as this is not dependent of any other file, then only main.cpp file is recompiled, and help.cpp and factorial.cpp are not.
在编译文件时, make 会检查其对象文件并比较时间戳。如果源文件的时间戳比对象文件新,则它会假定源文件已更改,然后生成新的对象文件。
While compiling a file, the make checks its object file and compares the time stamps. If source file has a newer time stamp than the object file, then it generates new object file assuming that the source file has been changed.
Avoiding Recompilation
一个项目可能包含数千个文件。有时,您可能已更改源文件,但未必需要重新编译所有依赖于该源文件的文件。例如,假设您向其他文件依赖的头文件添加宏或声明。 make 会保守地假定头文件的任何更改都需要重新编译所有依赖文件,但您知道它们不需要重新编译,您不愿浪费时间等待它们编译。
There may be a project consisting of thousands of files. Sometimes you may have changed a source file but you may not want to recompile all the files that depend on it. For example, suppose you add a macro or a declaration to a header file, on which the other files depend. Being conservative, make assumes that any change in the header file requires recompilation of all dependent files, but you know that they do not need recompilation and you would rather not waste your time waiting for them to compile.
如果您在更改头文件之前预见到该问题,可以使用 -t
标记。此标记会 make 不运行规则中的命令,而是通过更改目标文件的上次修改日期,将目标标记为最新。您需要遵循此程序−
If you anticipate the problem before changing the header file, you can use the `-t' flag. This flag tells make not to run the commands in the rules, but rather to mark the target up to date by changing its last-modification date. You need to follow this procedure −
-
Use the command `make' to recompile the source files that really need recompilation.
-
Make the changes in the header files.
-
Use the command `make -t' to mark all the object files as up to date. The next time you run make, the changes in the header files do not cause any recompilation.
如果你在某些文件确实需要重新编译时已经更改了头文件,那就太晚了。相反,您可以使用 -o file
标志,它将指定的文件标记为“旧”。这意味着文件本身不会被重制,并且没有任何其他内容会被重制。您需要遵循以下过程:
If you have already changed the header file at a time when some files do need recompilation, it is too late to do this. Instead, you can use the `-o file' flag, which marks a specified file as "old". This means, the file itself will not be remade, and nothing else will be remade on its account. you need to follow this procedure −
-
Recompile the source files that need compilation for reasons independent of the particular header file, with `make -o header file'. If several header files are involved, use a separate `-o' option for each header file.
-
Update all the object files with `make -t'.