zoukankan      html  css  js  c++  java
  • [Foward]Reduce Exe File Size

    There are something that can be improved about those mentioned in the above article.

    First, The built release exe file are dependent on the msvrct.dll(build with vc++6.0) or msvcr71.dll(build with vc++2003) etc.

    Second, the exe file is about 3K, but this can still be reduced to about 1K.

    Solutions:
    1. The program doesn't use any c runtime library functions, except WinMainCRTStartup(the default program entry point). So if we assign our own entry point function, we can strip the c runtime library completely.
    Improvements:
    (1). Remove the msvcrt.lib in the vc++'s linker/input entry.
    (2). Add "#pragma comment(linker, "/Entry:WinMain")" just below "#include "stdafx.h"".

    2. For this program we can merge the .text section with .rdata section.
    Improvements:
    Add the following statement just under "#include "stdafx.h"":
    #pragma comment(linker, "/Merge:.rdata=.text")

    So the souce code is like:

    #include "stdafx.h"


    #pragma comment(linker, "/Entry:WinMain")
    #pragma comment(linker, "/OPT:NOWIN98")
    #pragma comment(linker, "/Merge:.rdata=.text")

    int APIENTRY WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
    {
    // TODO: Place code here.
    MessageBoxW(NULL, L"Hello World", L"Hello World", MB_OK);
    return 0;
    }

    AOTHER SOLUTION

    Create another similar project workspace, i.e. another "Hello World" project.

    Select active build configuration to be "Win32 Release".

    Open project setting and select the "Link" tab. Remove all the library file names from the "Object/library modules:" edit box and type "MSVCRT.LIB kernel32.lib user32.lib". Press OK and compile, the final exe file size is reduced to 16K. You might get a linker warning like "LINK : warning LNK4098: default lib "LIBC" conflicts with use of other libs; use /NODEFAULTLIB:library". This can be avoided by clicking "Ignore all default libraries" from the Link tab of Project Settings.

    The further magic is done by using /ALIGN linker option. You can see MSDN for further details about this linker option. Again go to the "Project Settings", "Link" tab. Type /ALIGN:4096 in the Project Options edit box. Press OK and rebuild your project. The size is further reduced to 3K. The compiler generates a linker warning "LINK : warning LNK4108: /ALIGN specified without /DRIVER or /VXD; image may not run". If you have followed my instructions properly, your exe file should run properly. I played around with the /ALIGN linker directive and figured out that if you use /ALIGN:4096 the produced exe file runs properly.



    That's it, we have reduced size of our Hello World app from 24K to 3K. There are some other options that you can try in this regard:
    You can run some symbol stripper utility on the final exe file. It helps reducing size in many cases. One such utility can be found at neoworx.com.

    There is a utility named ASPack, this utility also helps in compressing your exe files up to 50%.

  • 相关阅读:
    生成随机端口函数
    于获得MFC窗口其它类指针的方法
    VC6.0中使用ADO操作Access数据库 (转)
    【原创】C++利用IXMLDOM解析XML文件。
    转帖:用MFC对话框做无闪烁图片重绘一一 程序设计: icemen
    C代码优化方案(转)
    【转】C++ Socket UDP "Hello World!"
    线程中使用UpdateData出错解决方法(转)
    C语言调试打印log函数。
    Windows Sockets 网络编程(三) —— WINDOWS SOCKETS 1.1 程序设计(转)
  • 原文地址:https://www.cnblogs.com/Tue/p/1617256.html
Copyright © 2011-2022 走看看