Writing Command Lines
Writing good, portable makefile files is a bit of an art. Skill comes with practice and experience. Here are some tips to get you started:
Depending upon your locale, naming your file Makefile instead of makefile can cause it to be listed first with ls. This makes it easier to find in a directory with many files.
Remember that command lines must start with a leading tab character. You cannot just indent the line with spaces, even eight spaces. If you use spaces, make exits with an unhelpful message about a “missing separator.”
Remember that
$is special to make. To get a literal$into your command lines, use$$. This is particularly important if you want to access an environment variable that isn’t a make macro. Also, if you wish to use the shell’s$$for the current process ID, you have to type it as$$$$.Write multiline shell statements, such as shell conditionals and loops, with trailing semicolons and a trailing backslash:
if [ -f specfile ] ; then \ ... ; \ else \ ... ; \ fiNote that the shell keywords
thenandelsedon’t need the semicolon. (What happens is that make passes the backslashes and the newlines to the shell. The escaped newlines are not syntactically important, so the semicolons are needed to separate the different parts of the command. This can be confusing. If you use a semicolon where you would normally put a newline in a shell script, things should work correctly.)Remember that each line is run in a separate shell. This means that commands ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access