/usr/share/doc/gccintro/gccintro/A-simple-makefile.html is in gccintro 1.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>An Introduction to GCC: A simple makefile</title>
<meta name="description" content="An Introduction to GCC: A simple makefile">
<meta name="keywords" content="An Introduction to GCC: A simple makefile">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Index.html#Index" rel="index" title="Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Compiling-a-C-program.html#Compiling-a-C-program" rel="up" title="Compiling a C program">
<link href="Linking-with-external-libraries.html#Linking-with-external-libraries" rel="next" title="Linking with external libraries">
<link href="Recompiling-and-relinking.html#Recompiling-and-relinking" rel="prev" title="Recompiling and relinking">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="A-simple-makefile"></a>
<div class="header">
<p>
Next: <a href="Linking-with-external-libraries.html#Linking-with-external-libraries" accesskey="n" rel="next">Linking with external libraries</a>, Previous: <a href="Recompiling-and-relinking.html#Recompiling-and-relinking" accesskey="p" rel="prev">Recompiling and relinking</a>, Up: <a href="Compiling-a-C-program.html#Compiling-a-C-program" accesskey="u" rel="up">Compiling a C program</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html#Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="A-simple-makefile-1"></a>
<h3 class="section">2.6 A simple makefile</h3>
<a name="index-makefile_002c-example-of"></a>
<a name="index-GNU-Make"></a>
<p>For those unfamiliar with <code>make</code>, this section provides a simple
demonstration of its use. Make is a program in its own right and can be
found on all Unix systems. To learn more about the GNU version of
<code>make</code> you will need to consult the <cite>GNU Make</cite> manual by
Richard M. Stallman and Roland McGrath (see <a href="Further-reading.html#Further-reading">Further reading</a>).
</p>
<a name="index-target_002c-in-makefile"></a>
<a name="index-dependency_002c-in-makefile"></a>
<a name="index-command_002c-in-makefile"></a>
<p>Make reads a description of a project from a <em>makefile</em> (by default,
called <samp>Makefile</samp> in the current directory). A makefile specifies
a set of compilation rules in terms of <em>targets</em> (such as
executables) and their <em>dependencies</em> (such as object files and
source files) in the following format:
</p>
<div class="example">
<pre class="example"><i>target</i>: <i>dependencies</i>
<i>command</i>
</pre></div>
<a name="index-tab_002c-in-makefiles"></a>
<a name="index-separator_002c-in-makefiles"></a>
<p>For each target, make checks the modification time of the corresponding
dependency files to determine whether the target needs to
be rebuilt using the corresponding command. Note that the
<code><i>command</i></code> lines in a makefile must be indented with a single
<tt class="key">TAB</tt> character, not spaces.
</p>
<a name="index-implicit-rules_002c-in-makefile"></a>
<a name="index-rules_002c-in-makefile"></a>
<a name="index-CFLAGS_002c-make-variable"></a>
<a name="index-CC_002c-make-variable"></a>
<a name="index-CXX_002c-make-variable"></a>
<a name="index-CXXFLAGS_002c-make-variable"></a>
<a name="index-CPPFLAGS_002c-make-variable"></a>
<a name="index-variables_002c-in-make"></a>
<p>GNU Make contains many default rules, referred to as <em>implicit</em>
rules, to simplify the construction of makefiles. For example, these
specify that <samp>.o</samp> files can be obtained from <samp>.c</samp> files by
compilation, and that an executable can be made by linking together
<samp>.o</samp> files. Implicit rules are defined in terms of <em>make
variables</em>, such as <code>CC</code> (the C compiler) and <code>CFLAGS</code> (the
compilation options for C programs), which can be set using
<code><i>VARIABLE</i>=<i>VALUE</i></code> lines in the makefile. For C++ the
equivalent variables are <code>CXX</code> and <code>CXXFLAGS</code>, while the make
variable <code>CPPFLAGS</code> sets the preprocessor options. The implicit and
user-defined rules are automatically chained together as necessary by
GNU Make.
</p>
<p>A simple <samp>Makefile</samp> for the project above can be written as
follows:
</p>
<div class="example">
<pre class="verbatim">CC=gcc
CFLAGS=-Wall
main: main.o hello_fn.o
clean:
rm -f main main.o hello_fn.o
</pre></div>
<p>The file can be read like this: using the C compiler <code>gcc</code>,
with compilation option <samp>-Wall</samp>, build the target executable
<code>main</code> from the object files <samp>main.o</samp> and <samp>hello_fn.o</samp>
(these, in turn, will be built via implicit rules from <samp>main.c</samp>
and <samp>hello_fn.c</samp>). The target <code>clean</code> has no dependencies
and simply removes all the compiled files.<a name="DOCF5" href="#FOOT5"><sup>4</sup></a> The option <samp>-f</samp> (force) on the <code>rm</code> command
suppresses any error messages if the files do not exist.
</p>
<p>To use the makefile, type <code>make</code>. When called with no arguments,
the first target in the makefile is built, producing the executable
<samp>main</samp>:
</p>
<div class="example">
<pre class="example">$ make
gcc -Wall -c -o main.o main.c
gcc -Wall -c -o hello_fn.o hello_fn.c
gcc main.o hello_fn.o -o main
$ ./main
Hello, world!
</pre></div>
<p>To rebuild the executable after modifying a source file, simply type
<code>make</code> again. By checking the timestamps of the target and dependency files,
make identifies the files which have changed and regenerates the corresponding
intermediate files needed to update the targets:
</p>
<div class="example">
<pre class="example">$ emacs main.c <span class="roman">(edit the file)</span>
$ make
gcc -Wall -c -o main.o main.c
gcc main.o hello_fn.o -o main
$ ./main
Hello, everyone!
</pre></div>
<p>Finally, to remove the generated files, type <code>make clean</code>:
</p>
<div class="example">
<pre class="example">$ make clean
rm -f main main.o hello_fn.o
</pre></div>
<p>A more sophisticated makefile would usually contain additional targets
for installation (<code>make install</code>) and testing (<code>make check</code>).
</p>
<p>The examples in the rest of this book are small enough not to need
makefiles, but the use of make is recommended for any larger programs.
</p>
<div class="footnote">
<hr>
<h4 class="footnotes-heading">Footnotes</h4>
<h3><a name="FOOT5" href="#DOCF5">(4)</a></h3>
<p>This assumes that
there is no file called <samp>clean</samp> in the current directory—see
the discussion of “phony targets” in the GNU Make manual for
details.</p>
</div>
<hr>
<div class="header">
<p>
Next: <a href="Linking-with-external-libraries.html#Linking-with-external-libraries" accesskey="n" rel="next">Linking with external libraries</a>, Previous: <a href="Recompiling-and-relinking.html#Recompiling-and-relinking" accesskey="p" rel="prev">Recompiling and relinking</a>, Up: <a href="Compiling-a-C-program.html#Compiling-a-C-program" accesskey="u" rel="up">Compiling a C program</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html#Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|