如何使 Delphi 编译的 EXE 变廋(150分)

  • 主题发起人 主题发起人 huizhang1
  • 开始时间 开始时间
用Install Package中的Build withruntime Package是可以使得
程序在形式上"变瘦"的,因为运行还需要DPL/BPL.但如果多个程序
在同一机器上当然有效果.

如果是数据库编程,Multi-tier也可以考虑,但EXE并不能变瘦,只是
需要安装的程序总量少了(没有了BDE)
 
;我对delphi变得程序变得越来越大也相当苦恼,
2.0时最小可编译成100多k,3.0时就是近200k,
如今4.0带Form的要280多k,如此发展怎么行?
变小的方法有,不写Form,用资源文件RES,和对话框。
最小可编译成20几k。
还有就是,重写delphi的原码,将一些不必要的功能去掉,
但这比较复杂。比如,在form中有好多属性,可能根本用不到,
如,dock、biMode等等。但编译时,这些功能也要编进去,也
就是这些功能在执行文件中有,但是不用,这当然会变大。
编译成动态的也会大幅度见效,只是不能在未装delphi的机器上用。
还有就只有等待delphi 的新版本,会加一些编译开关,或是弄几套
库文件,以适合不同功能和不同大小的应用程序。

 
Hehe, 您只要用ExePack for Windows之类的工具, 就可以让Delphi
的尺寸变为原来的一半, :)
另外, 如果您确实没有用到VCL, 也可以使代码的大小大幅度减小
 
Package 只能解决所使用的控件问题不能从根本上使EXE变廋, Baland pascal 在
link 时只是简单的将那些 dcu 文件串起来, exe 文件"胖"就胖在这些加肥了的
unit 文件上了。比如说 windows.pas 和 classes.pas 文件, 每一个 delphi 程
序必须引用, 但是对于一般的小的 APP 来说我们实际上只是用到了其中很少的一部
分。

前两天在网上看到一个廋 Delphi 的样例,抓回来一看整个一个 BPW,令人失望。
我一直想对 Delphi 自带的源文件做大手术,将他们大卸八块,但是工程量太大没时间来做。如果能够拆分后重新打包的话,我相信会使 exe 廋下来。
 
也许这篇Tips有点用:
不要用太多的Form,可能吗?

Reduce the .EXE-size of your app

Use CASE.. statments rather than IF..ELSE.. clauses.
In every .PAS-file that will be linked in your project, place the following line to the
top of your code:


{$D-,L-,O+,Q-,R-,Y-,S-}



Also add this line to your project source (.DPR).
{$D-} will prevent placing Debug info to your code.
{$L-} will prevent placing local symbols to your code.
{$O+} will optimize your code, remove unnecessary variables etc.
{$Q-} removes code for Integer overflow-checking.
{$R-} removes code for range checking of strings, arrays etc.
{$S-} removes code for stack-checking. USE ONLY AFTER HEAVY TESTING !
{$Y-} will prevent placing smybol information to your code.
After doing this, recompile the whole project - your .EXE-size should magically have
been reduced by 10-20 %..
If you link in graphics, they don't need to have more than 16 or 256 colors.
If the goal is really and only .EXE size, load your graphics by code ("manually").
If you include resource-files, they should only content the resources you really need
- and nothing more.
If you use Delphi 1, finally run W8LOSS.EXE on your .EXE file (not required for
32bit-Apps).
Delphi 2 produces larger .EXE sizes than Delphi 1 - 32 bit code demands its tribute..
Delphi 3 produces larger .EXE sizes than Delphi 2 - main reason: market pressure
leads to more "fundamental" support of "bells and whistles" - quality and
usefulness/efficiency/productivity doesn't seem to be a real criteria in M$-times...
check the "Show Hints" and "Show Warnings" options on the
Project|Options|Compiler page, then rebuild your project. It will show you every
variable and proc/func that isn't being used. You might be able to trim a little there,
too.
Clean your USES.. clauses for all unneeded units !
The so-called "SmartLinking" doesn't always remove all unused code. In a large
project you could possibly spare 100k in the EXE by that.
Place Bitmaps/Glyphs/etc. in DLL's instead of in *.RES/*.DCR files !
If you place fx some large bitmaps in a .RES, these will compiled into the EXE.
Remove the .RES-declarations, instead place them in a new project like this:


LIBRARY My_Extern_RESes;

USES EmptyUnit;

{$R MyRes1.RES}

{$R MyRes2.RES}

...

INITIALIZATION

END.



The unit AEmptyUnit is a completely empty unit. You have to use a unit like this
because any other unit will place unnecessary code in the final DLL. When you want
to use an image (and typically, you only want to load it once), you can do it like this
:


MyHandle := LoadLibrary('My_Extern_RESes.DLL');

TButton1.Glyph.Handle := LoadBitmap(MyHandle,'My_Glyph');



Placing Glyphs, Bitmaps, String-const's etc. in DLL's can really reduce a projects
EXE-size. (Tip came from David Konrad)
Basis rule: the more forms and units you add, the larger your exe becomes. When
you had one huge form, you only had one form class - even when you had 500+
things on it ! This creates only one runtime type information for that class. Now,
when you split this into 17 units, each class in that unit requires its own RTTI. Now,
this runtime information can get large, collectively, with a lot of redundant
information compared to when you only had one huge class. Also, if each of the 17
units had its own form, your end product must contain resource information for all 17
of those forms even though most of that information may be redundant. One reason
why this happens is because the Delphi compiler doesn't optimize resources - but I
don't know of any compiler that does. So, if you had two separate forms which are
identical in look, you'll have 2 copies of the resource in your .EXE.
This leaves some work for the programmer to be creative in maximizing resource
reuseability. (Tip came from Young Chung)
 
pegasus,

你说的那个 ExePack 工具能否给兄弟寄一份来,我只用过 ResStrim 工具,只能去
掉多余的 Resource。
 
我过去正好做过这样的工作,就我的经验,
编写大型系统时必须采取下面的办法,让EXE变瘦(特别是当EXE达到3-4兆!):
1、把常数尽量放在Form中(比如在Form中添一个ListBox用来存放这些常数)
2、把绝大部分Form资源和一些其他资源(位图、字串等)单独建立一个DLL
3、在自己的应用中动态掉用这些FORM和资源。
Form动态调用的方法自然是用ReadComponent系列的函数。
感兴趣吗?
 
各位朋友:
我现在已经找到了两个压缩工具,
One is general, the other is especial
for Delphi exe/dll.
If you want it, please send me a msg,
then I can just reply, attaching them. :)
 
Mail me and Display here, pegasus.
 
我手头有一个使delphi的Exe文件变瘦的程序,不知您感不感兴趣,要的话,
告诉我你的Mail地址.my mail:czydsp@263.net
 
如果大家指得是ASPack,为什么不给出个地址呢?
ftp://202.96.210.166:67/program/aspack.exe

但据说只是基本不影响原来程序,大家可要慎用哟!
^^^^
 
No!
My application is named Fat_To_Thin.Exe written by a Delphi
programmer from RU!

It just for Delphi32 Exe.
 
dsp兄:

我想强调的是我们有什么控件来源都应该贴出来让大家共享,
我一直是这么做的,遇到有人说不能上网/出国之类的原因,
才给人家发过去。而且最好发给版主一份,供大家下载。

当然,是否这样做是每个人的自由。
 
这类软件不可靠,在某些情况下,EXE无法运行!
 
本人的程序只是一个Exe,无源代码.而且在本人机器上,在哪儿Down的忘了.
共享的话没问题,只是怎么共享?本人新来的说!
发给哪个地址?Mail to me:czydsp@263.net


至于可不可靠,本人不敢定论.但是delphi的exe中含有许多无用信息这是大家
公认的,因此减肥的方法理论上应该有的!
 
To dw-wang:
Hehe, 我所说的专门用于Delphi的工具就是你说的那个, 因为我
自己也没能将它Down下来, 所以... :( (不知道怎么回事, 一down
那个文件, 就引起系统崩溃!!!)

另一个是通用工具, 应该没有稳定性的问题. 是我从朋友那里得
到的, 我马上就寄给版主

>我想强调的是我们有什么控件来源都应该贴出来让大家共享,
>我一直是这么做的,遇到有人说不能上网/出国之类的原因,
>才给人家发过去。而且最好发给版主一份,供大家下载。
Agree, hehe, you just tell me the URL to get examples of NetDDE, thanks a lot! *-^
 
来自pegasus的 <a href=/delphi/attachments/Wwp32112.zip>Wwp32112.zip</a>.
 
我下载了2次Wwp32112.zip,解包是均出错。有人成功过吗?
 
还真是有问题,pegasus, 请再送一次好吗?谢谢。
<hr>
请下载来自 dsp 的 <a href=/delphi/attachments/fat_to_thin.exe>fat_to_thin.exe</a>。
 
后退
顶部