Makefile: Difference between revisions

1,177 bytes added ,  15 years ago
Some generic intro into how make works.
[unchecked revision][unchecked revision]
(→‎Makefile Tutorial: - specific to GNU make)
(Some generic intro into how make works.)
Line 7:
 
The Makefile creates only one project-wide linker library, but it should be easy to expand it for multiple binaries/libraries.
 
== Basics ==
 
It is best practice to name your makefile <code>Makefile</code> (without an extension), because when ''make'' is executed without any parameters, it will by default look for that file in the current directory. It also makes the file show up prominently on top of listings, at least on Unix machines.
 
Generally speaking, a makefile consists of definitions, and rules.
 
A definition declares a variable, and assigns a value to it. Its overall syntax is ''VARIABLE := Value''.
 
A rule defines a ''target'', 0..n ''dependencies'', and 0..n ''commands''. The general idea is that ''make'' checks if the target (file) is there; if it isn't, or any of the dependencies is newer than the target, the commands are executed. The general syntax is:
 
<pre>
target: dependency
rule
</pre>
 
Note that the rule ''must be tab-indented''. If your editor environment is set to replace tabs with spaces, you have to undo that setting while editing a makefile.
 
What makes makefiles so hard to read, for the beginner, is that we are not looking at an imperative program here that is executed top-down; ''make'' reads the ''whole'' makefile, and then hops from rule to rule to satisfy whatever target you gave it on the command line.
 
I won't go into further details. This is not a man page, but a tutorial, so I will show you how a makefile is build, and the ideas behind each line.
 
== File Lists ==
It is best practice to name your makefile <code>Makefile</code> (without an extension), because when ''make'' is executed without any parameters, it will by default look for that file in the current directory.
 
First, I assemble various "file lists" which I need later in the Makefile.
448

edits