Vc6 generates the smallest EXE file

Source: Internet
Author: User

Compile the EXE as small as possible. The steps are as follows:
1. Discard the CRT.
CRT provides a large number of common functions. It can be said that C/C ++ programs will basically use it.
It also increases a lot of volume. Although it can be solved through dynamic link to external DLL, but since then there has been
A larger DLL dependency, so to lose weight, you must first open the CRT.
Add/MD to Cl compilation parameters, and add/nodefalib Lib: msvcrt. lib to link to avoid link
To CRT (I don't know the Lib File Name of the static CRT, So I first link it to dynamic, and then remove the corresponding msvcrt. Lib)

2. Reload new, new [], delete, delete []
C/C ++ programs need to dynamically allocate memory, and the CRT is lost before the program is compiled, it will find that there is a lot of memory allocated.
The error is reported because the CRT is removed and the compiler cannot find the corresponding function. Therefore, you need to write the memory allocation function by yourself.
The malloc of C and the new/new [] of C ++ are all allocated memory on the current stack. Therefore, you only need to write it again:

C/C ++ code
   typedef UINT size_t; 
void*malloc(size_t size)
{
return HeapAlloc (GetProcessHeap(),NULL,size);
}
void free (void*memblock)
{
HeapFree (GetProcessHeap(),NULL,memblock);
}
void*realloc(void*memblock,size_t size)
{
return HeapReAlloc (GetProcessHeap(),NULL,memblock,size);
}
void*operatornew(size_t count)
{
return malloc (count);
}
void*operatornew[](size_t count)
{
return malloc (count);
}
voidoperator delete(void* _Ptr) throw( )
{
free (_Ptr);
}
voidoperator delete[](void* _Ptr) throw( )
{
free (_Ptr);
}

3. Replacement of common functions.
Without CRT, many common functions cannot be used, and all self-rewriting will undoubtedly greatly increase the program generation.
The amount of code does not make much sense. Windows provides many common functions, all provided by the system DLL,
You don't need to use them.
A string processing function.
There are many alternatives to string processing functions. kernel32.dll provides the lstr *** function.
It can meet the needs of string processing. The sprintf function of C Format String is very useful, while user32.dll
The corresponding functions wsprintf/wvsprintf are also provided for replacement. It is a little troublesome to search functions without strings.
However, you cannot write a few lines of code, or simply use the more complete string processing provided by shlwapi. dll.
Library to replace.
B command line input/output functions.
This... the system does not really provide any corresponding functions, but it is not very complicated to use. It is written using the console API.
Several commonly used Getline and puts are enough.

4. Specify the entry point again.
If you ask the entry point of the VC program, it is estimated that many people will answer the question "Main/winmain.
Yes. This is the entry point of the program using the standard CRT, but the actual entry function is compiled inside the CRT.
After the initialization of the library is completed, call main/winmain. Since the CRT is removed, the linker will naturally issue an entry not found.
Function errors. The solution is simple. Use/entry: to specify a function. I still use main.
However, note that this main cannot contain any parameters.

5. Change the section alignment size.
The default alignment size of the CL compiler is 4 K (4096), which is a problem. Sometimes the compiled program does not have
Method execution. This is only a few experiments by myself. I set it to 512 (in fact, it can be smaller, but less than 512 of the program can not use UPX compression) and the program is a few K smaller.

6. Shell the exe.
Adding a shell to the program using the EXE shelling program is also a good way to reduce the volume.
I usually use UPX. If the preceding section alignment value is greater than 512, you can use UPX for Shell compression.
It can be compressed to about 66% of the original size.

7. Discount methods:

If you lose the CRT, it takes a lot of work to rewrite some functions, and some functions cannot be written by yourself (such as sin function, such as my
High-tech cainiao has a headache) but sometimes these functions are used, so there is a compromise:
Vc6 was launched in 98 years. Its Dynamic Link Library version DLL is msvcrt. dll, because of the early release, the installation rate of this DLL is very high.
(I used to use win98se in my school, so I don't need to mention the system after 98.) using it can basically ignore the lack of DLL during deployment.
So you can use the CRT function provided by this DLL. to use it, you must first find vc6, copy msvcrt. Lib in it, and change the name. For example:
Vcrt6.lib is then added to the Linked Library. There will be a library conflict prompt during compilation, and you can ignore it directly.
Some of the CRT libraries are capable of losing weight. You don't have to worry about missing libraries, and you don't have to specify the entry points again. However, I found that
In this way, the compiled program will be a little larger than the total loss of CRT, so it is just a compromise.

Next we will perform the following experiment:
Write a simple program:

C/C ++ code
   # Include <windows. h>
# Include <stdio. h>
Void main ()
{
Char * STR = newchar [250];
Sprintf (STR, "the current system has been running for % d milliseconds! ", Gettickcount ());
MessageBox (null, STR, STR, mb_iconinformation );
Delete [] STR;
}

Then compile the Code (the vc9 compiler does not change the section alignment value, but does not shell the Code)
Static CRT: 50kb
Dynamic Link CRT: 5.50kb
Use vc6 dynamic link CRT: 3.00 KB

You do not need to change the CRT at all:

C/C ++ code
   # Include <windows. h>
# Pragma comment (Lib, "user32.lib ")
# Pragma comment (Lib, "kernel32.lib ")
Void * operatornew [] (unsigned int size)
{
Return heapalloc (getprocessheap (), null, size );
}
Voidoperator Delete [] (void * memblock)
{
Heapfree (getprocessheap (), null, memblock );
}
# Define sprintf wsprintf
Void main ()
{
Char * STR = newchar [250];
Sprintf (STR, "the current system has been running for % d milliseconds! ", Gettickcount ());
MessageBox (null, STR, STR, mb_iconinformation );
Delete [] STR;
}

Compilation parameters are also relatively long:
CL/MD msgbox. CPP/link/nodefaultlib: msvcrt. lib/entry: Main
Size after compilation: 2.00kb
Add/align: 16 after compilation size: 960 bytes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.