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

博客指出C/C++可执行文件大小不同的原因,如编译器版本、磁盘扇区设置、版本类型、C运行时库版本等。还给出代码优化建议,如使用NOWIN98开关、最小化大小优化、使用动态链接库等,经测试可大幅减小代码大小。
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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值