wiki:make:patterns_rules
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| wiki:make:patterns_rules [2014/08/18 09:59] – Add code styling to prevent latex render jensv | wiki:make:patterns_rules [2022/07/21 06:59] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ===Automatic Variables=== | ||
| + | |||
| + | We have some redundancy in our current makefile. We repeat the dependencies in the command. We can remove this redundancy by using the somewhat cryptic '' | ||
| + | |||
| + | #This is a makefile for analyzing molecule data | ||
| + | result.dat: sorted_lengths.txt | ||
| + | ./middle.sh $^ -5 -3 | ||
| + | | ||
| + | sorted_lengths.txt: | ||
| + | ./ | ||
| + | |||
| + | There are other automatic variables as well: for example, '' | ||
| + | | ||
| + | If we delete our '' | ||
| + | |||
| + | $ rm result.dat | ||
| + | $ rm sorted_lengths.txt | ||
| + | $ make | ||
| + | |||
| + | ./ | ||
| + | ./middle.sh sorted_lengths.txt -5 -3 | ||
| + | |||
| + | Who can explain why now all the .pdb files are in the command instead of *.pdb? | ||
| + | |||
| + | ===Macros=== | ||
| + | |||
| + | We may want to change the lines that are being selected by the middle.sh. It can be cumbersome to search through a large makefile for an individual command to edit a part of it. | ||
| + | We can do this by defining a macro, just as we would define a constant or variable in a program. Here's our Makefile with a macro defined and used: | ||
| + | |||
| + | #This is a makefile for analyzing molecule data | ||
| + | | ||
| + | END_LINE=-5 | ||
| + | LINES=-3 | ||
| + | | ||
| + | result.dat: sorted_lengths.txt | ||
| + | ./middle.sh $^ ${END_LINE} ${LINES} | ||
| + | | ||
| + | sorted_lengths.txt: | ||
| + | ./ | ||
| + | | ||
| + | The definition looks like definitions in most programming languages: | ||
| + | the macros are called '' | ||
| + | To use the macro, we put a dollar sign in front of it (just as we would do in the shell) and wrap its name in curly brackets. | ||
| + | This tells Make to insert the macro' | ||
| + | |||
| + | This is certainly a step forward: | ||
| + | now, when we want to move our Makefile from one machine to another, we only have to change one definition in one place. | ||
| + | |||
| + | ==Parenthesizing Macros in Make== | ||
| + | |||
| + | We have to put curly brackets or parentheses around a macro' | ||
| + | If we do, Make will interpret it as '' | ||
| + | Since we probably don't have a macro called M, '' | ||
| + | so '' | ||
| + | |||
| + | |||
| + | Why? | ||
| + | |||
| + | |||
| + | To make a long story short, it's another wart left over from its history. | ||
| + | Almost everyone trips over it occasionally, | ||
| + | |||
| + | It's common practice to use macros to define all the flags that tools need, so that if a tool is invoked in two or more actions, it's passed a consistent set of flags. | ||
| + | |||
| + | |||
| + | We can also define a macro like '' | ||
| + | |||
| + | $ make LINES=-4 | ||
| + | |||
| + | This is almost always a bad idea, though. We have to remember to type the definition each time, and we have to type it correctly each time. This isn't too bad with just one definition, but is infeasible when there are half a dozen. There' | ||
| + | |||
| + | ===Pattern Rules=== | ||
| + | |||
| + | Sometimes we want to create an output for each one of our inputs. This often happens in compiling where each source code file is turned into a compiled binary file. | ||
| + | |||
| + | For example a common rule for a fortran code is: | ||
| + | |||
| + | %.o: %.f90 | ||
| + | | ||
| + | |||
| + | |||
| + | In this rule, '' | ||
| + | |||
| + | Here all binary files .o depend on all fortran 90 source code files .f90. | ||
| + | '' | ||
| + | |||
| + | Why is the make wildcard '' | ||
| + | |||
| + | We will see a detailed example of this in the next section on managing the building of programs with make. | ||
| + | |||
| + | |||
| + | |||