惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
A
About on SuperTechFans
J
Java Code Geeks
G
Google Developers Blog
L
LangChain Blog
小众软件
小众软件
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
博客园 - 【当耐特】
IT之家
IT之家
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
阮一峰的网络日志
阮一峰的网络日志
V
V2EX
博客园 - Franky
博客园_首页
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
Vercel News
Vercel News
B
Blog
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - JohnsonFeng

JavaScript 性能优化 --By Google V8 Team Manager 在C++中嵌入JavaScript——Google V8 JavaScript Engine使用体验 介绍相似图片搜索:TinEye VC环境OpenSSL编译笔记 Nginx Server 加密库相关资源 C++中使用Expat解析XML 在C++应用中通过Luabind使用lua脚本(一) - JohnsonFeng - 博客园 在动作捕捉系统中使用加速度和倾角传感器 游戏需求分析 慎用CMFCToolBar::CreateEx IOCP在网络应用中常见错误分析 Direct3D程序调试利器 PIX Win 简介 数据自解压简单原理 编写和调试Shader程序(1) 生成均匀“随机数”,一种生成均匀分布数字的简单方法 Visual C++ Profile的简单使用方法 C++简单的函数计时方法 [转]About SIGGRAPH
[Foward]Reduce Exe File Size
JohnsonFeng · 2009-12-04 · via 博客园 - JohnsonFeng

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%.