home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 May / PCP163A.iso / Runimage / Cbuilder4 / Examples / WinTools / MAKE.TXT < prev    next >
Encoding:
Text File  |  1999-01-26  |  18.1 KB  |  403 lines

  1. MAKE Help
  2.  
  3. Using MAKE
  4.  
  5. MAKE.EXE is a command-line utility that helps you manage project compilation and link cycles. MAKE is not inherently tied to compiling and linking, but is a more generic tool for executing commands based on file dependencies. MAKE helps you quickly build projects by compiling only the files you have modified since the last compilation. In addition, you can set up rules that specify how MAKE should deal with the special circumstances in your builds.
  6.  
  7. MAKE Basics
  8.  
  9. MAKE uses rules you write along with its default settings to determine how it should compile the files in your project. For example, you can specify when to build your projects with debug information and to compile your .OBJ files only if the date/time stamps of a source file is more recent than the .OBJ itself. If you need to force the compilation of a module, use TOUCH.EXE to modify the time stamp of one of the moduleÆs dependents.
  10.  
  11. In an ASCII makefile, you write explicit and implicit rules to tell MAKE how to treat the files in your project; MAKE determines if it should execute a command on a file or set of files using the rules you set up. Although your commands usually tell MAKE to compile or link a set of files, you can specify nearly any operating system command with MAKE.
  12.  
  13. The general syntax for MAKE is
  14.  
  15. MAKE [options...] [target[target]]
  16.  
  17. options
  18.  
  19. are MAKE options that control how MAKE works
  20.  
  21. target
  22.  
  23. is the name of the target listed in the makefile that you want to build
  24.  
  25. You must separate the MAKE command and the options and target arguments with spaces. When specifying targets, you can use wildcard characters (such as * and ?) to indicate multiple files. To get command-line help for MAKE, type MAKE -?.
  26. Default MAKE actions
  27.  
  28. When you issue a MAKE command, MAKE looks for the file BUILTINS.MAK, which contains the default rules for MAKE (use the -r option to ignore the default rules). MAKE looks for this file first in the current directory, then in the directory where MAKE.EXE is stored. After loading BUILTINS.MAK, MAKE looks in the current directory for a file called MAKEFILE or MAKEFILE.MAK (use the -f option to specify a file other than MAKEFILE). If MAKE canÆt find either of these files, it generates an error message.
  29.  
  30. After loading the makefile, MAKE tries to build only the first explicit target listed in the makefile by checking the time and date of the dependent files of the first target. If the dependent files are more recent than the target file, MAKE executes the commands to update the target.
  31. If one of the first targetÆs dependent files is used as a target elsewhere in the makefile, MAKE checks that targetÆs dependencies and builds it before building the first target. This chain reaction is called a linked dependency.
  32.  
  33. If something during the build process fails, MAKE deletes the target file it was building. Use the .precious directive if you want MAKE to keep the target when a build fails.
  34. You can stop MAKE after issuing the MAKE command by pressing Ctrl+Break or Ctrl+C.
  35.  
  36.  
  37. BUILTINS.MAK
  38.  
  39. The file BUILTINS.MAK contains standard rules and macros that MAKE uses when it builds the targets in a makefile. To ignore this file, use the -r MAKE option.
  40. Here is the default text of BUILTINS.MAK:
  41.  
  42. #
  43. # Borland C++ - (C) Copyright 1997 by Borland International
  44. #
  45.  
  46. CC       = bcc32
  47. RC       = brcc32
  48. AS       = tasm32
  49.  
  50. .asm.obj:
  51.       $(AS) $(AFLAGS) $&.asm
  52.  
  53. .c.exe:
  54.       $(CC) $(CFLAGS) $&.c
  55.  
  56. .c.obj:
  57.       $(CC) $(CFLAGS) /c $&.c
  58.  
  59. .cpp.exe:
  60.       $(CC) $(CFLAGS) $&.cpp
  61.  
  62. .cpp.obj:
  63.       $(CC) $(CPPFLAGS) /c $&.cpp
  64.  
  65. .rc.res:
  66.       $(RC) $(RFLAGS) /r $&
  67.  
  68. .SUFFIXES: .exe .obj .asm .c .res .rc
  69.  
  70. !if !$d(BCEXAMPLEDIR)
  71. BCEXAMPLEDIR = $(MAKEDIR)\..\EXAMPLES
  72.  
  73. !endif
  74.  
  75.  
  76. About makefiles
  77.  
  78. A makefile is an ASCII file that contains the set of instructions that MAKE uses to build a certain project. Although MAKE assumes your makefile is called MAKEFILE or MAKEFILE.MAK, you can specify a different makefile name with the -f option.
  79. MAKE either builds the target(s) you specify with the make command or it builds the first target it finds in the makefile To build more than a single target, use a symbolic target in your makefile.
  80. Makefiles can contain
  81.  
  82.     Comments (precede with a number sign [#])
  83.     Explicit and implicit rules
  84.     Macros
  85.     Directives
  86.  
  87. Symbolic targets
  88.  
  89. A symbolic target forces MAKE to build multiple targets in a makefile. When you specify a symbolic target, the dependency line lists all the targets you want to build (a symbolic target basically uses linked dependencies to build more than one target).
  90. For example, the following makefile uses the symbolic target AllFiles to build both FILE1.EXE and FILE2.EXE:
  91.  
  92. AllFiles: file1.exe file2.exe  #Note that AllFiles has no commands
  93. file1.exe: file1.obj
  94.     bcc32 file1.obj
  95. file2.exe: file2.obj
  96.     bcc32 file2.obj
  97.  
  98. Rules for symbolic targets
  99.  
  100. Observe the following rules when you use symbolic targets:
  101.  
  102.     Do not type a line of commands after the symbolic target line.
  103.     A symbolic target must have a unique name; it cannot be the name of a file in your current directory.
  104.     Symbolic target names must follow the operating system rules for naming files.
  105.  
  106. MAKE options
  107.  
  108. You can use command-line options to control the behavior of MAKE. MAKE options are case-sensitive and must be preceded with either a hyphen (-) or slash (/).
  109. The general syntax for MAKE is
  110.  
  111. MAKE [options...] [target[target]]
  112.  
  113. options
  114.  
  115. are MAKE options that control how MAKE works
  116.  
  117. target
  118.  
  119. is the name of the target listed in the makefile that you want to build
  120.  
  121. You must separate the MAKE command and the options and target arguments with spaces. When specifying targets, you can use wildcard characters (such as * and ?) to indicate multiple files. To get command-line help for MAKE, type MAKE -?.
  122.  
  123. For example, to use a file called PROJECTA.MAK as the makefile, type MAKE -fPROJECTA.MAK. Many of the command-line options have equivalent directives that you can use within the makefile.
  124.  
  125. Option    Description
  126.  
  127.  
  128.  
  129. -a    Checks dependencies of include files and nested include files associated with .OBJ files and updates the .OBJ if the .h file changed. See also -c.
  130. -B    Builds all targets regardless of file dates.
  131. -c    Caches autodependency information, which can improve MAKE speed. Use with -a. Do not use this option if MAKE modifies include files (which can happen if you use TOUCH in the makefile or if you create header or include files during the MAKE process).
  132.  
  133. -Dmacro    Defines macro as a single character, causing an expression !ifdef macro written in the makefile to return true.
  134. [-D]macro=[string]    Defines macro as string. If string contains any spaces or tabs, enclose string in quotation marks. The -D is optional.
  135. -ddirectory    Specifies the drive and directory that MAKER (the real mode version of MAKE) uses when it swaps out of memory. This option must be used with -S. MAKE ignores this option.
  136.  
  137. -e    Ignores a macro if its name is the same as an environment variable (MAKE uses the environment variable instead of the macro).
  138. -ffilename    Uses filename or filename.MAK instead of MAKEFILE (a space after -f is optional).
  139. -h or -?    Displays MAKE options. Default settings are shown with a trailing plus sign.
  140. -Idirectory    Searches for include files in the current directory first, then in directory you specify with this option.
  141.  
  142. -i    Ignores the exit status of all programs run from the makefile and continues the build process.
  143. -l    Enables the use of long comment lines (on by default).
  144. -K    Keeps temporary files that MAKE creates (MAKE usually deletes them).
  145. -m    Displays the date and time stamp of each file as MAKE processes it.
  146. -N    Causes MAKE to mimic Microsoft's NMAKE.
  147. -n    Prints the MAKE commands but does not perform them, this is helpful for debugging makefiles.
  148.  
  149. -p    Displays all macro definitions and implicit rules before executing the makefile.
  150. -q    Returns 0 if the target is up-to-date and nonzero if it is not (for use with batch files).
  151. -r    Ignores any rules defined in BUILTINS.MAK.
  152. -S    Swaps MAKER out of memory while commands are executed, reducing memory overhead and allowing compilation of large modules. MAKE ignores this option.
  153. -s    Suppresses onscreen command display (silent).
  154.  
  155. -Umacro    Undefines the previous macro definition of macro.
  156. -Wfilename    Writes MAKE to filename, updating all non-string options.
  157.  
  158. Using TOUCH
  159.  
  160. TOUCH.EXE updates a file's date stamp so that it reflects your systemÆs current time and date.
  161. Sometimes you may need to force a target to be recompiled or rebuilt even though you haven't changed its source files. One way to do this is to use the TOUCH utility to update the time stamp of one or more of the targetÆs dependency files. To touch a file (or files), type the following at the command prompt:
  162.  
  163.     touch [options] filename [filename...]
  164.  
  165. If TOUCH cannot find a file that matches the name you specify, it creates a zero-length file with the correct date stamp. To suppress automatic file creation, use the -c option.
  166.  
  167. Because TOUCH is a 32-bit executable, it accepts long file names. In addition, you can use file names that contain the wildcard characters * and ? to ôtouchö more than a single file at a time. Use the -s option to update matching files in all subdirectories.
  168.  
  169. Note:    Before you use TOUCH, make sure your system's internal clock is set correctly.
  170.  
  171. TOUCH options
  172.  
  173. TOUCH.EXE supports several command-line options:
  174.  
  175. Option    Description
  176.  
  177.  
  178.  
  179. -c    DonÆt generate file if it doesnÆt already exist.
  180. -dmm-dd-yy    Sets the date of the file to the specified date.
  181. -rfilename    Sets the time and date of files to match those of filename.
  182. -h    Displays help information (same as typing TOUCH without options or file names).
  183. -s    Recurses through subdirectories
  184. -thh:mm:ss    Sets the time of the file to the specified time.
  185. -v    Verbose mode. Shows each file TOUCHed.
  186.  
  187. Explicit and implicit rules
  188.  
  189. You write explicit and implicit rules to instruct MAKE how to build the targets in your makefile. In general, these rules are defined as follows:
  190.  
  191. Explicit rules are instructions for specific files.
  192. Implicit rules are general instructions for files without explicit rules.
  193.  
  194. All the rules you write follow this general format:
  195.  
  196. Dependency line
  197.     Command line
  198.  
  199. While the dependency line has a different syntax for explicit and implicit rules, the command line syntax stays the same for both rule types.
  200.  
  201. MAKE supports multiple dependency lines for a single target, and a single target can have multiple command lines. However, only one dependency line should contain a related command line. For example:
  202.  
  203. Target1: dependent1 dep2 dep3 dep4 dep5
  204. Target1: dep6 dep7 dep8
  205.     bcc32 -c $**
  206.  
  207. Explicit rule syntax
  208.  
  209. Explicit rules specify the instructions that MAKE must follow when it builds specific targets. Explicit rules name one or more targets followed by one or two colons. One colon means one rule is written for the target(s); two colons mean that two or more rules are written for the target(s).
  210. Explicit rules follow this syntax:
  211.  
  212. target [target...]:[:][{path}] [dependent[s]...]
  213.     [commands]
  214.  
  215.  
  216. target
  217.  
  218. specifies the name and extension of the file to be built (a target must begin a line in the makefile-you cannot precede the target name with spaces or tabs). To specify more than one target, separate the target names with spaces or tabs. Also, you cannot use a target name more than once in the target position of an explicit rule.
  219.  
  220. path
  221.  
  222. is a list of directories that tells MAKE where to find the dependent files. Separate multiple directories with semicolons and enclosed the entire path specification in braces.
  223.  
  224. dependent
  225.  
  226. is the file (or files) whose date and time MAKE checks to see if it is newer than target. Each dependent file must be preceded by a space. If a dependent appears elsewhere in the makefile as a target, MAKE updates or creates that target before using the dependent in the original target (this in known as a linked dependency).
  227.  
  228. commands
  229.  
  230. are any operating system command or commands. You must indent the command line by at least one space or tab, otherwise they are interpreted as a target. Separate multiple commands with spaces.
  231.  
  232. If a dependency or command line continues on the following line, use a backslash (\) at the end of the first line to indicate that the line continues. For example,
  233.  
  234. MYSOURCE.EXE: FILE1.OBJ\          #Dependency line
  235.     FILE3.OBJ                     #Dependency line continued
  236.     bcc32 file1.obj file3.obj     #Command line
  237.  
  238. Single targets with multiple rules
  239.  
  240. A single target can have more than one explicit rule. To specify more than a single explicit rule, use a double colon (::) after the target name. The following example shows targets with multiple rules and commands.
  241.  
  242. .cpp.obj:
  243.     bcc32 -c -ncobj $<
  244.  
  245. .asm.obj:
  246.     tasm  /mx $<, asmobj\
  247.  
  248. mylib.lib :: f1.obj f2.obj      #double colon specifies multipe rules
  249.     echo Adding C files
  250.     tlib mylib -+cobjf1 -+cobjf2
  251.  
  252. mylib.lib :: f3.obj f4.obj
  253.     echo Adding ASM files
  254.     tlib mylib -+asmobjf3 -+asmobjf4
  255.  
  256. Implicit rule syntax
  257.  
  258. An implicit rule specifies a general rule for how MAKE should build files that end with specific file extensions. Implicit rules start with either a path or a period. Their main components are file extensions separated by periods. The first extension belongs to the dependent, the second to the target.
  259. If implicit dependents are out-of-date with respect to the target, or if the dependents don't exist, MAKE executes the commands associated with the rule. MAKE updates explicit dependents before it updates implicit dependents.
  260.  
  261. Implicit rules follow this basic syntax:
  262.  
  263. [{source_dir}].source_ext[{target_dir}].target_ext:
  264.     [commands]
  265.  
  266.  
  267. source_dir
  268.  
  269. specifies the directory (or directories) containing the dependent files. Separate multiple directories with a semicolon.
  270.  
  271. .source_ext
  272.  
  273. specifies the dependent filename extension.
  274.  
  275. target_dir
  276.  
  277. specifies the directory where MAKE places the target files. The implicit rule will only be used for targets in this directory. Without specifying a target directory, targets from any directory will match the implicit rule.
  278.  
  279. .target_ext
  280.  
  281. specifies the target filename extension. Macros are allowed here.
  282.  
  283. : (colon)
  284.  
  285. marks the end of the dependency line.
  286.  
  287. commands
  288.  
  289. are any operating system command or commands. You must indent the command line by at least one space or tab, otherwise they are interpreted as a target.
  290.  
  291. If two implicit rules match a target extension but no dependent exists, MAKE uses the implicit rule whose dependent's extension appears first in the .SUFFIXES list.
  292.  
  293. Explicit rules with implicit commands
  294.  
  295. A target in an explicit rule can get its command line from an implicit rule. The following example shows an implicit rule followed by an explicit rule without a command line.
  296.  
  297. .c.obj:
  298.  
  299.     bcc32 -c $<   #This command uses a macro $< described later
  300.  
  301.  
  302.  
  303. myprog.obj:       #This explicit rule uses the command: bcc32 -c myprog.c
  304.  
  305. The implicit rule command tells MAKE to compile MYPROG.C (the macro $< replaces the name myprog.obj with myprog.c).
  306. Command syntax
  307.  
  308. Commands immediately follow an explicit or implicit rule and must begin on a new line with a space or tab.
  309. Commands can be any operating system command, but they can also include MAKE macros, directives, and special operators that your operating system wonÆt recognize (however, note that | can't be used in commands). Here are some sample commands:
  310.  
  311. cd..
  312.  
  313. bcc32 -c mysource.c
  314.  
  315. COPY *.OBJ C:\PROJECTA
  316.  
  317. bcc32 -c $(SOURCE)     #Macros are explained later in the chapter.
  318.  
  319. Commands follow this general syntax:
  320.  
  321. [prefix...] commands
  322.  
  323. Command prefixes
  324.  
  325. Commands in both implicit and explicit rules can have prefixes that modify how MAKE treats the commands. The following table lists the prefixes you can use in makefiles:
  326.  
  327. Prefix    Description
  328.  
  329.  
  330.  
  331. @    Don't display the command while it's being executed.
  332. -num    Stop processing commands in the makefile when the exit code returned from command exceeds the integer num. Normally, MAKE aborts if the exit code is nonzero. No space is allowed between - and num.
  333. -    Continue processing commands in the makefile, regardless of the exit codes they return.
  334. &    Expand either the macro $**, which represents all dependent files, or the macro $?, which represents all dependent files stamped later than the target. Execute the command once for each dependent file in the expanded macro.
  335.  
  336. !    Will behave like the & prefix.
  337.  
  338. Using @
  339.  
  340. The following command uses the @ prefix, which prevents MAKE from displaying the command onscreen.
  341.  
  342. diff.exe : diff.obj
  343.     @bcc32 diff.obj
  344.  
  345. Using -num and -
  346.  
  347. The -num and - prefixes control the makefile processing when errors occur. You can choose to continue with the MAKE process if an error occurs or you can specify a number of errors to tolerate.
  348.  
  349. In the following example, MAKE continues processing if BCC32 returns errors:
  350.  
  351. target.exe : target.obj
  352. target.obj : target.cpp
  353.     -bcc32 -c target.cpp
  354.  
  355. Using &
  356.  
  357. The & prefix issues a command once for each dependent file. It is especially useful for commands that don't take a list of files as parameters. For example,
  358.  
  359. copyall : file1.cpp file2.cpp
  360.     © $** c:\temp
  361.  
  362. invokes COPY twice as follows:
  363.  
  364. copy file1.cpp c:\temp
  365. copy file2.cpp c:\temp
  366.  
  367. Without the & modifier, MAKE would call COPY only once. Note: the & prefix only works with $** and $! macros.
  368. MAKE command operators
  369.  
  370. While you can use any operating system command in a MAKE command section, you can also use the following special operators:
  371.  
  372. Operator    Description
  373.  
  374.  
  375.  
  376. <    Use input from a specified file rather than from standard input
  377. >    Send the output from command to file
  378. >>    Append the output from command to file
  379. <<    Create a temporary inline file and use its contents as standard input to command. Also, create temporary response file when -N is used. Note: this is only for use with NMAKE.
  380. &&    Create a temporary response file and insert its name in the makefile
  381. delimiter    Use delimiters with temporary response files. You can use any character other than # as a delimiter. Use << and && as a starting and ending delimiter for a temporary file. Any characters on the same line and immediately following the starting delimiter are ignored. The closing delimiter must be written on a line by itself.
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.