PDA

View Full Version : the smallest fpc program



AirPas
09-11-2011, 09:23 AM
i've tried to make the smallest fpc program without compression , this is what i did


program test;
begin
end.

//---compilation ---
fpc test.pas -O3 -XsX -CX -Sd

i got 30kb

so any one else have got less than 30kb ?

(with delphi i got 3.5 kb , with mini system and sysinit units)
i don't know if fpc has system units like in delphi !!

JSoftware
09-11-2011, 12:02 PM
For FPC the smallest RTL you can get(without ugly hacks or using Crinkler) is this:

system.pas


unit system;

interface

type
HResult = longint;

implementation

procedure FPC_INITIALIZEUNITS; nostackframe; [public, alias: 'FPC_INITIALIZEUNITS'];
begin
end;

procedure FPC_DO_EXIT; nostackframe; [public, alias: 'FPC_DO_EXIT'];
begin
end;

end.


sysinitpas.pas


unit sysinitpas;

interface

implementation

procedure PASCALMAIN; external name 'PASCALMAIN';

procedure ExitProcess(ExitCode: longint); stdcall; external 'kernel32.dll' name 'ExitProcess';

procedure Entry; [public, alias: '_mainCRTStartup'];
begin
PASCALMAIN;
ExitProcess(0);
end;

end.


fpintres.pas


unit fpintres;

interface

implementation

end.


With Crinkler you can easily get below 1kb if you craft some proper RTL units

AirPas
09-11-2011, 01:59 PM
hi jsoftware
thanks , but how could you get this compiled together !
i mean the commandline

i used -O3 -XsX -CX -Sd but i only got system.o

Colin
09-11-2011, 03:19 PM
does FPC not remove unused code, variable and consts????

in my assembler/compiler, i have include 10,000 lines of code, and if my main file does nothing it will be the minimum exe size, why does FPC use 30kb that is ridiculous....

in ziron, even a messagebox app is 2kb



program WIN32GUI 'Test';

#include 'kernel32.zir';
#include 'user32.zir';

MessageBox(0, 'Hello World!', 'Hello', MB_OK);

ExitProcess(0);


I'd be interested in what FPU adds so much to the exe to be 30kb!!!!!!!! :O

the minimum size note on windows (PE) is 1kb, since you need 512 bytes for headers and 512 for your code.



program WIN32GUI 'Test';

#include 'kernel32.zir';
#include 'user32.zir';
#include 'strings.zir';
#include 'gdi32.zir';

function Unused1() {
#repeat 100000:
eax = eax; //useless code to make the exe huge :) (not really since it will be discarded)
#end
}

MessageBox(0, 'Hello World!', 'Hello', MB_OK);

ExitProcess(0);


this compiles with an equiv of 101873 lines of code and is still only 2kb, i can add error checking too which takes it to 3.5kb......

JSoftware
09-11-2011, 04:42 PM
You have to compile the units first.

fpc -O3 -Us system
fpc -O3 fpintres
fpc -O3 sysinitpas
fpc -O3 -XsX -CX yourcode

JSoftware
09-11-2011, 04:44 PM
does FPC not remove unused code, variable and consts????

in my assembler/compiler, i have include 10,000 lines of code, and if my main file does nothing it will be the minimum exe size, why does FPC use 30kb that is ridiculous....

You forget that FPC has a lot of internals that needs initialization in a stock RTL. Multithreaded heap management, code page conversion tables for strings, wide string managers, thread managers, stack checking, command line parsing, virtual methods for TObject, etc

AirPas
10-11-2011, 07:43 AM
thanks for your help ,looks interesting now , MessageBox becomes 542b !! after compressing with upx and com

code_glitch
16-11-2011, 06:49 PM
Aside from a 30kb executeable being limited in functionality - UPX is a very good bet IMO. Although I admit I find 700kb ok and 70kb to be very small for me. You could always do some ASM8086 on linux - virtually no size at all.

marcov
19-11-2011, 10:33 PM
Also read the FPC wiki page about EXE sizes: http://wiki.freepascal.org/Size_Matters

Most importantly the "why bother?" bit.