您的位置:首页 > 编程语言

如何减少可执行文件的代码

2005-10-26 11:07 281 查看
   减少exe(dll)的代码,主要从几个方面能够入手:
  1.代码质量.Consider refactoring when you try to copy & paste code. 拷贝代码不仅是  造成代码的难以修改和维护,而且也增加最后可执行文件的大小.
  2.尽量的使用系统的动态链接库,比如kernel32.dll 等各个版本的系统必备的DLL. 象有些dll就不  行,例如msvcrt.dll在 win98上就没有. 但是项目使用到别的库经常是没法避免的,比如STL.
  3.最重要还是编译器选项(特别是链接器选项)优化. 用AppWizards建立一个Hello,world的GUI Win32程序,不加入任何代码,Release编译后也有40KB. 用Denpendency walker打开看看, 发现Kernel32.dll中引用了很多函数,但是实际上你的代码里面都没有引用到,那是因为默认链接器是把C Runtime library静态链接到你的代码里面去了,而你可能并没有使用到任何C Runtime Library的函数 .因此第一步就是在linker选项里面勾上ignore default libraries(这将使得链接器不在默认链接CRT). 然后加上你需要引用的库,例如kernel32.lib, 另外还需要设置EntryPoint(因为默认使用的是C Runtime Libray中的_mainCRTStartup 或者_WinMainCRTStartup函数). 第二步,加上/OPT:REF /OPT:ICF /OPT:NOWIN98 最后一个参数一般都能让exe进一步减少.另外是段合并(merge section)的优化,即把.rdata .text段都合并到.data段里面,但是这个优化并不推荐. 最后就是对齐的优化, /FILEALIGN(只能用在VC6上面),通过减少这个值能够去掉代码中由于对齐而产生的多余代码. 详细参考可以见参考资料第二个链接.
  4. exe压缩器. aspack,upxshell是用得最多的exe压缩器,通常可以把可执行文件减少一半左右
 
下面是最小的win32程序,VC6下面编译后只有480字节.
#include <windows.h>
 
// Make section alignment really small
#pragma comment(linker, "/FILEALIGN:16")
#pragma comment(linker, "/ALIGN:16")
 
// Merge sections
#pragma comment(linker, "/MERGE:.rdata=.data")
#pragma comment(linker, "/MERGE:.text=.data")
#pragma comment(linker, "/MERGE:.reloc=.data")
 
// Favour small code
#pragma optimize("gsy", on)
 
// Single entrypoint
int WinMainCRTStartup()
{
return 0;
}
 
上面的代码是从一篇文章中COPY过来的,见后面的参考.做一个win32 GUI hello,world,上面的法则仍然适用.用AppWizards生成一个Hello,world以后,把所有多余的全部去掉,例如资源文件. 甚至函数调用也不用了全部合并到WinMain中去(因为通过/OPT:NOWIN98和/FILEALIGN设置后,即使增加减少一丁点的代码也能够立即反应最后的EXE大小中去,而这些函数都只调用了一次,因此去掉了),生成的代码1.14KB. 如果再去掉WM_PAINT中的画Hello,world的代码,最后大小为1.04KB, 并且只依赖于kernel32.dll 和user32.dll

// MiniWinGUI.cpp : Defines the entry point for the application.
//
// Windows Header Files:
#include <windows.h>

#pragma comment(linker, "/FILEALIGN:16")
#pragma comment(linker, "/ALIGN:16")

#pragma comment(linker, "/OPT:REF")
#pragma comment(linker, "/OPT:ICF")
#pragma comment(linker, "/OPT:NOWIN98")

// Merge sections
#pragma comment(linker, "/MERGE:.rdata=.data")
#pragma comment(linker, "/MERGE:.text=.data")
#pragma comment(linker, "/MERGE:.reloc=.data")

// Favour small code
#pragma optimize("gsy", on)

#pragma comment(linker, "/ENTRY:WinMain")

// Global Variables:
HINSTANCE hInst;                                // current instance
TCHAR * szTitle = "MiniWinGUI";

// Foward declarations of functions included in this code module:
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
//    hInst = hInstance; // Store instance handle in our global variable

    WNDCLASSEX wcex;
   
    wcex.cbSize = sizeof(WNDCLASSEX);
   
    wcex.style            = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = (WNDPROC)WndProc;
    wcex.cbClsExtra        = 0;
    wcex.cbWndExtra        = 0;
    wcex.hInstance        = hInstance;
    wcex.hIcon            = NULL;
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = NULL;
    wcex.lpszClassName    = szTitle;
    wcex.hIconSm        = NULL;
   
    RegisterClassEx(&wcex);
   
   
    HWND hWnd;
   
   
    hWnd = CreateWindow(szTitle,szTitle, WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
   

    if (!hWnd)
    {
        return FALSE;
    }

    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);
   

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
   
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   
    switch (message)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐