外文文献—Linux编程指南.doc
《外文文献—Linux编程指南.doc》由会员分享,可在线阅读,更多相关《外文文献—Linux编程指南.doc(16页珍藏版)》请在三一办公上搜索。
1、附录 英文翻译第一部分 英文原文Linux_ProgrammingKurt WallCHAPTER 4 Project Management Using GNU makeIn this chapter, we take a long look at make, a tool to control the process of building (orrebuilding) software. make automates what software gets built, how it gets built, andwhen it gets built, freeing the program
2、mer to concentrate on writing code.Why make?For all but the simplest software projects, make is essential. In the first place, projects composed of multiple source files typically require long, complex compiler invocations.make simplifies this by storing these difficult command lines in the makefile
3、, which thenext section discusses.make also minimizes rebuild times because it is smart enough to determine which fileshave changed, and thus only rebuilds files whose components have changed. Finally,make maintains a database of dependency information for your projects and so can verifythat all of
4、the files necessary for building a program are available each time you start abuild.Writing MakefilesSo, how does make accomplish these magical feats? By using a makefile. A makefile is a text file database containing rules that tell make what to build and how to build it. A rule consists of the fol
5、lowing: A target, the “thing” make ultimately tries to create A list of one or more dependencies, usually files, required to build the target A list of commands to execute in order to create the target from the specified dependenciesWhen invoked, GNU make looks for a file named GNUmakefile, makefile
6、, or Makefile,in that order. For some reason, most Linux programmers use the last form, Makefile.Makefile rules have the general form target : dependency dependency .commandcommand.The Linux Programming ToolkitPART Itarget is generally the file, such as a binary or object file, that you want created
7、. Dependency is a list of one or more files required as input in order to create target. The commands are the steps, such as compiler invocations, necessary to create target. Unless specified otherwise, make does all of its work in the current working directory. If this is all too abstract for you,
8、I will use Listing 4.1 as an example. It is the makefile for building a text editor imaginatively named editor.LISTING 4.1 SIMPLE MAKEFILE ILLUSTRATING TARGETS, DEPENDENCIES, AND COMMANDS1 editor : editor.o screen.o keyboard.o2 gcc -o editor editor.o screen.o keyboard.o34 editor.o : editor.c editor.
9、h keyboard.h screen.h5 gcc -c editor.c67 screen.o : screen.c screen.h8 gcc -c screen.c910 keyboard.o : keyboard.c keyboard.h11 gcc -c keyboard.c1213 clean :14 rm editor *.oTo compile editor, you would simply type make in the directory where the makefile exists. Its that simple.This makefile has five
10、 rules. The first target, editor, is called the default targetthis is the file that make tries to create. editor has three dependencies, editor.o, screen.o, and keyboard.o; these three files must exist in order to build editor. Line 2 (the line numbers do not appear in the actual makefile; they are
11、merely pedagogic tools) is the command that make will execute to create editor. As you recall from Chapter 3, “Using GNU cc,” this command builds an executable named editor from the three object files.The next three rules (lines 411) tell make how to build the individual object files.Project Managem
12、ent Using GNU makeUSING GNUMAKEThe first character in a command must be the tab character; eight spaces will not suffice. This often catches people unaware, and can be a problem if your preferred editor “helpfully” translates tabs to eight spaces. If you try to use spaces instead of a tab, make disp
13、lays the message “Missing separator” and stops. Here is where makes value becomes evident: ordinarily, if you tried to build editorusing the command from line 2, gcc would complain loudly and ceremoniously quit if the dependencies did not exist. make, on the other hand, after seeing that editor requ
14、ires these other files, verifies that they exist and, if they dont, executes the commands on lines 5, 8, and 11 first, then returns to line 2 to create the editor executable. Of course, if the dependencies for the components, such as keyboard.c or screen.h dont exist, make will also give up, because
15、 it lacks targets named, in this case, keyboard.c and screen.h.“All well and good,” youre probably thinking, “but how does make know when to rebuild a file?” The answer is stunningly simple: If a specified target does not exist in a place where make can find it, make (re)builds it. If the target doe
16、s exist, make compares the timestamp on the target to the timestamp of the dependencies. If one or more of the dependencies is newer than the target, make rebuilds the target, assuming that the newer dependency implies some code change that must be incorporated into the target.More About RulesIn thi
17、s section, I will go into more detail about writing makefile rules. In particular, I cover creating and using phony targets, makefile variables, using environment variables and makes predefined variables, implicit rules, and pattern rules.Phony TargetsIn addition to the normal file targets, make all
18、ows you to specify phony targets. Phony targets are so named because they do not correspond to actual files. The final target in Listing 4.1, clean, is a phony target. Phony targets exist to specify commands that make should execute. However, because clean does not have dependencies, its commands ar
19、e not automatically executed. This follows from the explanation of how make works: upon encountering the clean target, make sees if the dependencies exist and, because clean has no dependencies, make assumes the target is up to date. In order to build this target, you have to type make clean. In our
20、 case, clean removes the editor executable and its constituent object files. You might create such a target if you wanted to create and distribute a source-code tarball to your users or to start a build with a clean build tree. If, however, a file named clean happened to exist, make would see it. Ag
21、ain, because it has no dependencies, make would assume that it is up to date and not execute the commands listed on line 14. To deal with this situation, use the special make target .PHONY.The Linux Programming ToolkitAny dependencies of the .PHONY target will be evaluated as usual, but make will di
22、sregard the presence of a file whose name matches one of .PHONYs dependencies and execute the corresponding commands anyway. Using .PHONY, our sample makefile would look like: 1 editor : editor.o screen.o keyboard.o2 gcc -o editor editor.o screen.o keyboard.o34 editor.o : editor.c editor.h keyboard.
23、h screen.h5 gcc -c editor.c67 screen.o : screen.c screen.h8 gcc -c screen.c910 keyboard.o : keyboard.c keyboard.h11 gcc -c keyboard.c1213.PHONY : clean1415 clean :16 rm editor *.oVariablesTo simplify editing and maintaining makefiles, make allows you to create and use variables. A variable is simply
24、 a name defined in a makefile that represents a string of text; this text is called the variables value. Define variables using the general form: VARNAME = some_text .To obtain VARNAMEs value, enclose it in parentheses and prefix it with a $:$(VARNAME)VARNAME expands to the text on the right-hand si
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 外文 文献 Linux 编程 指南
链接地址:https://www.31ppt.com/p-2388197.html