OSDN Git Service

Publish FAQ note on reducing size of executables.
[mingw/website.git] / exesize.html
1 <!DOCTYPE HTML><!--
2  *
3  * exesize.html
4  *
5  * HOWTO avoid production of excessively large executable files.
6  *
7  *
8  * $Id$
9  *
10  * Written by Keith Marshall <keith@users.osdn.me>
11  * Copyright (C) 2008, 2021, MinGW.OSDN Project
12  *
13  *
14  * Redistribution and use in source and 'compiled' forms (SGML, HTML,
15  * PDF, PostScript, RTF, etc) with or without modification, are permitted
16  * provided that the following conditions are met:
17  *
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer as
20  *    the first lines of this file, unmodified.
21  *
22  * 2. Redistributions in compiled form (transformed to other DTDs,
23  *    converted to PDF, PostScript, RTF and other formats) must
24  *    reproduce the above copyright notice, this list of conditions
25  *    and the following disclaimer in the documentation and/or other
26  *    materials provided with the distribution.
27  *
28  * THIS DOCUMENTATION IS PROVIDED BY THE MINGW.OSDN PROJECT "AS IS"
29  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
30  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE MINGW.OSDN PROJECT, OR
32  * ITS CONTRIBUTORS, BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
39  * DAMAGE.
40  *
41  *
42  * Note: this page assumes browser support for the following numeric
43  * HTML entity codes:
44  *
45  *    &#8197;   fixed width 1/4 em space
46  *    &#8209;   non-breaking hyphen
47  *    &#8216;   typographic left (opening) single quote
48  *    &#8217;   typographic apostrophe/right (closing) single quote
49  *    &#8220;   typographic left (opening) double quote
50  *    &#8221;   typographic right (closing) double quote
51  *
52 -->
53 <script class="masthead">
54 /* Script fragment, to assign titles specific to this page; this is
55  * encapsulated within the "masthead", where such titles are displayed,
56  * to ensure that whatever page content may follow will be correctly
57  * positioned, relative to the masthead content.
58  */
59  set_page("title", "MinGW Compiler Suite User Guide");
60  set_page("subtitle", "HOWTO Reduce the Size of MinGW Executable Files");
61 </script><!-- masthead -->
62 <div class="masthead" style="display: none">
63 <p class="byline">Posted: 21-Oct-2008, by mingwadmin; Last Update: 22-Nov-2021</p>
64 </div><!-- masthead -->
65 <div class="overlapped" id="introduction">
66 <h3>Introduction</h3>
67 <p>There are a number of reasons why object files,
68 executable files,
69 and libraries may be larger than expected;
70 principal among these are:
71 </p>
72 <ul>
73   <li>Overheads due to inclusion of debugging information;</li>
74   <li>C++ exception handling and run-time type information overheads;</li>
75   <li>C++ template class,
76     standard template library,
77     and standard C++ library overheads.
78   </li>
79 </ul>
80 <p>Below,
81 we discuss techniques for reducing the impact of these overheads,
82 and ultimately,
83 a technique for compressing executable files,
84 in order to minimize file size.
85 </p>
86 </div><!-- introduction -->
87
88 <div class="overlapped" id="debug-info">
89 <h3>Avoiding Inclusion of Debugging Information</h3>
90 <p>Perhaps,
91 the most common reason for executables being larger than expected,
92 is that they include <em>debugging information</em>&hairsp;;
93 (this is added to the object modules,
94 when source files are compiled with GCC&#8217;s
95 &#8220;<code>-g</code>&#8221; option).
96 Even when your own source files are compiled
97 <em>without</em>&hairsp; the &#8220;<code>-g</code>&#8221; option,
98 libraries which are linked into your executable may have been compiled with it;
99 (this may be the case,
100 even for the system libraries which are distributed with MinGW).
101 </p>
102 <p>To exclude debugging information from your executable,
103 simply use the &#8220;<code>strip</code>&#8221; command to remove it:
104 </p>
105 <pre class="box-out vt">
106 C:\project&gt; <kbd>strip example.exe</kbd>
107 </pre>
108 <p>Alternatively,
109 use GCC&#8217;s &#8220;<code>-s</code>&#8221; option when linking:
110 </p>
111 <pre class="box-out vt">
112 C:\project&gt; <kbd>gcc -s example.o -o example.exe</kbd>
113 </pre>
114 </div><!-- debug-info -->
115
116 <div class="overlapped" id="rtti">
117 <h3>Reducing C++ Exception Handling and Run&#8209;Time Type Information Overheads</h3>
118 <p>By default,
119 when compiling C++ code,
120 GCC will compile in support for handling of C++ exceptions,
121 and for interpretation of run&#8209;time type information (RTTI).
122 </p>
123 <p>If your application does not use either of these,
124 then you may choose to eliminate the overhead incurred by inclusion
125 of the supporting infrastructure code,
126 by compiling and linking with GCC&#8217;s
127 &#8220;<code>-fno&#8209;exceptions</code>&#8221;,
128 and &#8220;<code>-fno&#8209;rtti</code>&#8221; options,
129 to eliminate the C++ exception handling,
130 and RTTI interpretation overheads,
131 respectively.
132 </p>
133 </div><!-- rtti -->
134
135 <div class="overlapped" id="stl">
136 <h3>Understanding the Effect of C++ Template Classes and the C++ Standard Library</h3>
137 <p>When you use C++ template classes,
138 either defined by yourself,
139 or imported from the C++ Standard Template Library (STL),
140 the compiler generates code separately for each instantiation
141 (e.g.&nbsp;<code>vector&lt;int&gt;</code> and <code>vector&lt;string&gt;</code>),
142 so the total code size can increase significantly.
143 Other elements of the standard C++ library,
144 such as <code>iostreams</code> classes,
145 exception handling classes,
146 and classes such as <code>std::string</code> introduce further overheads,
147 which may seem large in comparison to the size of small applications,
148 (such as &#8220;<code>Hello World</code>&#8221; type programs).
149 </p>
150 <p>In practice,
151 these overheads are unavoidable;
152 however,
153 they are generally of constant size,
154 and thus,
155 will normally not be significant in comparison to the size
156 of more realistic (typically larger) C++ application programs.
157 </p>
158 </div><!-- stl -->
159
160 <div class="overlapped" id="upx">
161 <h3>Using UPX to Compress Executable Files</h3>
162 <p><a rel="noopener noreferrer" target="_blank"
163  href="https://upx.github.io/">UPX (the Ultimate Packer for eXecutables)
164 </a>is a tool which,
165 notwithstanding the avoidance of undesirable code generator overheads,
166 as described above,
167 compresses executable files to achieve further reduction of file size.
168 It works for the executable file formats of several operating systems,
169 including 32&#8209;bit MS&#8209;Windows,
170 and is free for use with <em>all</em>&hairsp; applications,
171 (even those which are distributed commercially);
172 it uses an efficient compression algorithm,
173 producing compressed files which remain executable,
174 and are rapidly decompressed <em>in&#8209;place</em>,&hairsp;
175 when invoked.
176 </p>
177 <p>For most effective size reduction,
178 UPX may deployed using <em>one</em>&hairsp; of the following invocations,
179 (acting on the original <em>uncompressed</em>&hairsp;
180 <code>example.exe</code>):
181 </p>
182 <pre class="box-out vt">
183 C:\project&gt; <kbd>upx --brute example.exe</kbd>
184 C:\project&gt; <kbd>upx --ultra-brute example.exe</kbd>
185 C:\project&gt; <kbd>upx --best example.exe</kbd>
186 </pre>
187 <p>to compress <code>example.exe</code> <em>in&#8209;place</em>.&hairsp;
188 (Do note that,
189 whereas the in&#8209;place <em>decompression</em>&hairsp; process
190 can be performed quite rapidly,
191 the <em>compression</em>&hairsp; may require a considerably longer time;
192 this is not generally a problem,
193 since compression must be performed only once,
194 for each newly generated executable file,
195 whereas the relatively much quicker decompression must be repeated
196 every time such a compressed executable file is run).
197 </p>
198 </div><!-- upx -->
199
200 <!-- $RCSfile$: end of file -->