1.5 Basic Makefile Syntax

     

Now that you have a basic understanding of make you can almost write your own makefile s. Here we'll cover enough of the syntax and structure of a makefile for you to start using make .

Makefile s are usually structured top-down so that the most general target, often called all , is updated by default. More and more detailed targets follow with targets for program maintenance, such as a clean target to delete unwanted temporary files, coming last. As you can guess from these target names , targets do not have to be actual files, any name will do.

In the example above we saw a simplified form of a rule. The more complete (but still not quite complete) form of a rule is:

   target    1    target    2    target    3  :   prerequisite    1    prerequisite    2    command    1    command    2    command    3  

One or more targets appear to the left of the colon and zero or more prerequisites can appear to the right of the colon . If no prerequisites are listed to the right, then only the target(s) that do not exist are updated. The set of commands executed to update a target are sometimes called the command script , but most often just the commands .

Each command must begin with a tab character. This (obscure) syntax tells make that the characters that follow the tab are to be passed to a subshell for execution. If you accidentally insert a tab as the first character of a noncommand line, make will interpret the following text as a command under most circumstances. If you're lucky and your errant tab character is recognized as a syntax error you will receive the message:

 $  make  Makefile:6: *** commands commence before first target.  Stop. 

We'll discuss the complexities of the tab character in Chapter 2.

The comment character for make is the hash or pound sign, . All text from the pound sign to the end of line is ignored. Comments can be indented and leading whitespace is ignored. The comment character does not introduce a make comment in the text of commands. The entire line, including the and subsequent characters, is passed to the shell for execution. How it is handled there depends on your shell.

Long lines can be continued using the standard Unix escape character backslash (\). It is common for commands to be continued in this way. It is also common for lists of prerequisites to be continued with backslash. Later we'll cover other ways of handling long prerequisite lists.

You now have enough background to write simple makefile s. Chapter 2 will cover rules in detail, followed by make variables in Chapter 3 and commands in Chapter 5. For now you should avoid the use of variables , macros, and multiline command sequences.



Managing Projects with GNU make
Managing Projects with GNU Make (Nutshell Handbooks)
ISBN: 0596006101
EAN: 2147483647
Year: 2003
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net