1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Compilation (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Compilation (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Compilation (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="index.html" rel="up" title="Top">
|
---|
33 | <link href="Directing-Compilation.html" rel="next" title="Directing Compilation">
|
---|
34 | <link href="Further-Reading.html" rel="prev" title="Further Reading">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Compilation"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Directing-Compilation.html" accesskey="n" rel="next">Directing Compilation</a>, Previous: <a href="Floating-Point-in-Depth.html" accesskey="p" rel="prev">Floating Point in Depth</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Compilation-1"></span><h2 class="chapter">29 Compilation</h2>
|
---|
64 | <span id="index-object-file"></span>
|
---|
65 | <span id="index-compilation-module"></span>
|
---|
66 | <span id="index-make-rules"></span>
|
---|
67 |
|
---|
68 | <p>Early in the manual we explained how to compile a simple C program
|
---|
69 | that consists of a single source file (see <a href="Compile-Example.html">Compile Example</a>).
|
---|
70 | However, we handle only short programs that way. A typical C program
|
---|
71 | consists of many source files, each of which is a separate
|
---|
72 | <em>compilation module</em>—meaning that it has to be compiled
|
---|
73 | separately.
|
---|
74 | </p>
|
---|
75 | <p>The full details of how to compile with GCC are documented in xxxx.
|
---|
76 | Here we give only a simple introduction.
|
---|
77 | </p>
|
---|
78 | <p>These are the commands to compile two compilation modules,
|
---|
79 | <samp>foo.c</samp> and <samp>bar.c</samp>, with a command for each module:
|
---|
80 | </p>
|
---|
81 | <div class="example">
|
---|
82 | <pre class="example">gcc -c -O -g foo.c
|
---|
83 | gcc -c -O -g bar.c
|
---|
84 | </pre></div>
|
---|
85 |
|
---|
86 | <p>In these commands, <samp>-g</samp> says to generate debugging information,
|
---|
87 | <samp>-O</samp> says to do some optimization, and <samp>-c</samp> says to put
|
---|
88 | the compiled code for that module into a corresponding <em>object
|
---|
89 | file</em> and go no further. The object file for <samp>foo.c</samp> is called
|
---|
90 | <samp>foo.o</samp>, and so on.
|
---|
91 | </p>
|
---|
92 | <p>If you wish, you can specify the additional options <samp>-Wformat
|
---|
93 | -Wparenthesis -Wstrict-prototypes</samp>, which request additional warnings.
|
---|
94 | </p>
|
---|
95 | <p>One reason to divide a large program into multiple compilation modules
|
---|
96 | is to control how each module can access the internals of the others.
|
---|
97 | When a module declares a function or variable <code>extern</code>, other
|
---|
98 | modules can access it. The other functions and variables in
|
---|
99 | a module can’t be accessed from outside that module.
|
---|
100 | </p>
|
---|
101 | <p>The other reason for using multiple modules is so that changing
|
---|
102 | one source file does not require recompiling all of them in order
|
---|
103 | to try the modified program. Dividing a large program into many
|
---|
104 | substantial modules in this way typically makes recompilation much faster.
|
---|
105 | </p>
|
---|
106 | <span id="index-linking-object-files"></span>
|
---|
107 | <p>After you compile all the program’s modules, in order to run the
|
---|
108 | program you must <em>link</em> the object files into a combined
|
---|
109 | executable, like this:
|
---|
110 | </p>
|
---|
111 | <div class="example">
|
---|
112 | <pre class="example">gcc -o foo foo.o bar.o
|
---|
113 | </pre></div>
|
---|
114 |
|
---|
115 | <p>In this command, <samp>-o foo</samp> species the file name for the
|
---|
116 | executable file, and the other arguments are the object files to link.
|
---|
117 | Always specify the executable file name in a command that generates
|
---|
118 | one.
|
---|
119 | </p>
|
---|
120 | <p>Normally we don’t run any of these commands directly. Instead we
|
---|
121 | write a set of <em>make rules</em> for the program, then use the
|
---|
122 | <code>make</code> program to recompile only the source files that need to
|
---|
123 | be recompiled.
|
---|
124 | </p>
|
---|
125 |
|
---|
126 | <hr>
|
---|
127 | <div class="header">
|
---|
128 | <p>
|
---|
129 | Next: <a href="Directing-Compilation.html" accesskey="n" rel="next">Directing Compilation</a>, Previous: <a href="Floating-Point-in-Depth.html" accesskey="p" rel="prev">Floating Point in Depth</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
130 | </div>
|
---|
131 |
|
---|
132 |
|
---|
133 |
|
---|
134 | </body>
|
---|
135 | </html>
|
---|