Slimming your execution file [original]
Online, there are many green software, not only powerful, but also the size of the software itself is very small. Usually
Only dozens of k. Then how did they do how little software? Now I tell my handle
How do you slim your executive files by modifying the compilation options.
First look at a most typical procedure:
#include
int main ()
{
Printf ("Hello, World! / N");
Return 0;
}
The above procedure is called typical because he has the following content:
1, system function call: printf
2, there is a static data segment
Ok, now put this file in VisualStudio6.0 to compile, see how big is the file.
1. Open the helloworld.cpp file with VisualStudio6.0 and press F7 directly. Then click OK to generate
Project file, then compile. After completing the compilation, look at the execution file in the debug directory.
Small, 172,096bytes.
2. The debug file just compiled, now modify it into the release file. Choose Win32 Release, then
Compilation. Look at the size of the execution file, now it has become 40,960bytes. It seems that the Release version is better than Debug
Small.
3, check code optimization: Discover optimization of execution files is Maximize Speed. So modified into minimize
Size look. Re-compile, get the size of the execution file is: 40,960bytes. It seems that there is no change in size.
. In fact, this is because our code itself is too small, resulting in even if it changed.
4. Think about the main function of our program is booted by the CRT class library. In our current settings, by
The system default compilation connection is taken (the default is SINGLE THREAD, STIC
Library, so, in our execution file, contains the binary code of the CRT. Ok, modify
Translation options: C / C => category: code generation => Use run-time
Library: Mutithreaded DLL. Compilation: The execution file size becomes 16,384bytes.
5, the settings just have really good, and the size of the execution file is reduced to 16K. Now use UltraEdit
Look at the executive files. The result was shocked: it is basically 0. It seems that this is reduced.
It is necessary. I know that the executive file has its own code segment, data segment, etc., each segment is also mining
Use the compiler to default. Ok, let's modify the size of the segment:
5.1 One of the connection options is / opt: nowin98, meaning the size of the segment becomes Win2000 adaptation
of. Compilation and see: Wow, becomes 2,560byte. It seems that this option does make a small file smaller N more.
5.2 Is there any special in the check option. Discover / align: XX can also shrink
small. Look at the files compiled by UltraEdit, and found that the size of each segment is
It is an integer multiple of 4K. It seems that / align: XX also has reduced trend. Try again: Add a connection option:
/ align: 16 (This size is already the smallest). Look at the results: 1,408bytes. Efforts
Hazard, now the code is smaller.
5.3 Recall now, executing the file size has a data segment, executing code segment, etc., if these paragraphs are
Get up, is it a redundancy between the paragraphs? Try again: Add option: / merge: .data = .text /merge:.rdata=.text. Take a look at the file size: 1,328bytes. Really
Very good.
6. The settings just have really good, it seems to reach the limit we want. But look back, if
Will it be smaller if there is no CRT library? In fact, this is actually like this. Add connection options: / entry:
Main, point the entrance address directly to our main function. Get 592bytes.
In the end we got our last size 592bytes. I think this may be we can compile through the compiler.
The smallest code comes out.
in conclusion:
Through the above steps, we understand how to modify those who compile the connection options to reach the execution file slim.
of. However, in general, in our release file, it does not need such a small execution file.
If you want to reach slimming, modify it to library: mutithreaded DLL and add / opt: nowin98
It is a good choice. Other options are compiled or more or less warnings, and with
Executive files with those compiled options are not necessarily available on each platform.
In addition: If your execution file is more big, even if these settings are still more big, you can also pass some
EXE file compression tools are compressed. For example, UPX, etc. This is no longer told here.
In the shortcomings of the above part, please refer to it.
Tuyi 2004/09/20