Here are some useful hints for reduce your C/C++ code dramatically

类别:编程语言 点击:0 评论:0 推荐:
Possible reasons are:
1. Although the 2 Visual C++ you are using are the same version, the service pack could be different, thus the compilers are actually different;

2. The disk sector size could be set differently, although the real sizes are the same, the apparent sizes could differ, you can find this out by checking the real sizes of the 2 executables;

3. The 2 executable files could be that one is release version, which is smaller, and the other is debug version, which is bigger. If you use MFC or ATL, the difference will be very big, but if you use pure API, the difference is not that much;

4. The version of C run time (CRT) library could be different on the 2 machines, and that results in different size of code linked to the executable;

Here are some useful hints for reduce your C/C++ code dramatically:

1. Use the magical NOWIN98 switch (if your application is not to be compiled on Win98/95 platforms). Simply put the following line at the beginning of your .cpp file or .h file:
#pragma comment(linker, "/OPT:NOWIN98");

2. Use the minimize size optimization in the project settings;

3. Use MSVCRT.dll instead of the statically linked CRT library. In Visual C++, in the project settings, go to link section, in general category, in "object/library modules", delete the unnecessary .lib files, for example the odbc.lib (if you don't want to use odbc), and then put msvcrt.lib;

4. If you don't want to use the Structured Exception Handling in C++, you need to turn it off in project settings;

5. If you don't need the Run Time Type Information (RTTI) in C++, you need to turn it off in project settins;

6. If you use class inheritance, and you don't want the overhead of virutal function dispatch table (vtable), you can use the magicall __declspec(novtable) modifier. ATL uses this a lot;

7. If you are writing a tiny dll for fast downloading, then you need to careful of the function naming, make the function names as short as possible and then use the alias in full-sized name in the C++ header file, because the ASCII decription of the function names and signatures will occupy a significant amount of space;

8. Most of all, do not allow the compiler to generate debug information.

Test these tricks with a simple "hello world" Win32 console program, the original size without optimization is 40KB, after all possible optimization tricks, the size is 3KB!

本文地址:http://com.8s8s.com/it/it22971.htm