Recent

Author Topic: smallest fpc program size  (Read 5328 times)

fcu

  • Jr. Member
  • **
  • Posts: 89
smallest fpc program size
« on: July 17, 2017, 11:55:34 am »
Hi
how to generate the samllest exe size with fpc without embedded rtl ?

with mingw only passing -nosdtlib and rename main function to _main  gives me 1.5kb exe

thanks

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: smallest fpc program size
« Reply #1 on: July 17, 2017, 12:55:31 pm »
Use smartlinking and strip debugging. In theory also optimization for size, but that is peanuts.  This should give you something 30kbish, and that is currently the best you can do on windows without modifying the RTL.

The smallest files DOS com files I guess. But probably the 16-bit dos RTL also counts as "embedded".

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: smallest fpc program size
« Reply #2 on: July 17, 2017, 01:17:19 pm »
Note there is always a trade off:
- embedded: you can count in bytes, not Kb, to turn e.g. a led on/off
- full blown OS's (more useful, more generic) FPC already encapsulates a lot that e.g. c does not.
- what are your requirements?
- What is your platform? Because that makes a huge difference.

What Marco wrote is just about right for both Linux and Windows: 30K ish. But for embedded devices you can go as low as just a couple of 100's of bytes.
If you are skilled enough, you could just use embedded mode with just the startup code and always end up within ~ 1000 bytes.
Stripping the system to what's required....
« Last Edit: July 17, 2017, 01:19:45 pm by Thaddy »
Specialize a type, not a var.

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: smallest fpc program size
« Reply #3 on: July 17, 2017, 03:11:37 pm »
- embedded: you can count in bytes, not Kb, to turn e.g. a led on/off
Mentioning mingw and exe I don't think he was refering to target embedded  ;)
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

fcu

  • Jr. Member
  • **
  • Posts: 89
Re: smallest fpc program size
« Reply #4 on: July 17, 2017, 03:46:52 pm »
forgot to mention iam using fpc for win32 ;)
smartlinking and strip are on by default if you just type : fpc programname.pas , which gives 31kb for empty app .

ok i think i found how to get the smallest size .
fpc needs : system.pas , sysinitpas.pas , fpintres.pas
after emptying those files and let only the Entry procedure i get 3.5kb for a MessageBox App ;)

you can try your self

system.pas
Code: Pascal  [Select][+][-]
  1. unit system;
  2.  
  3. interface
  4.  
  5. type
  6.         HResult = longint;
  7. implementation
  8.  
  9. procedure FPC_INITIALIZEUNITS;  [public, alias: 'FPC_INITIALIZEUNITS'];
  10. begin
  11. end;
  12.  
  13. procedure FPC_DO_EXIT; [public, alias: 'FPC_DO_EXIT'];
  14. begin
  15. end;
  16.  
  17. end.
  18.  

sysinitpas.pas
Code: Pascal  [Select][+][-]
  1. unit sysinitpas;
  2.  
  3. interface
  4.  
  5. implementation
  6.  
  7. procedure PASCALMAIN; external name 'PASCALMAIN';
  8.  
  9. procedure ExitProcess(ExitCode: longint); stdcall; external 'kernel32.dll' name 'ExitProcess';
  10.  
  11. procedure Entry; [public, alias: '_mainCRTStartup'];
  12. begin
  13.    PASCALMAIN;
  14.    ExitProcess(0);
  15. end;
  16.  
  17. end.
  18.  

fpintres.pas
Code: Pascal  [Select][+][-]
  1. unit fpintres;
  2.  
  3. interface
  4.  
  5. implementation
  6.  
  7. end.
  8.  

main.pas
Code: Pascal  [Select][+][-]
  1. program test;
  2. type pchar = ^char;
  3.  
  4. function MessageBox(hwnd:longint; msg,title:pchar; op:longint):longint; stdcall external 'user32.dll' name 'MessageBoxA';
  5.  
  6. begin
  7.         MessageBox(0,'FPC 3.5kb App',';)',0);
  8. end.
  9.  

compiling with :
Code: Pascal  [Select][+][-]
  1. fpc -Os -Us system
  2. fpc -Os sysinitpas
  3. fpc -Os -XsX -CX main.pas
  4.  


marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: smallest fpc program size
« Reply #5 on: July 17, 2017, 03:51:32 pm »
forgot to mention iam using fpc for win32 ;)

(You might also try the nativent target)

fcu

  • Jr. Member
  • **
  • Posts: 89
Re: smallest fpc program size
« Reply #6 on: July 17, 2017, 04:10:00 pm »
forgot to mention iam using fpc for win32 ;)

(You might also try the nativent target)

how ?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: smallest fpc program size
« Reply #7 on: July 17, 2017, 04:28:36 pm »
Afaik there are no releases, so you have to build it yourself. But maybe it is more system oriented than win32.

E.g. with 

make clean cycle OS_TARGET=nativent

in compiler/

fcu

  • Jr. Member
  • **
  • Posts: 89
Re: smallest fpc program size
« Reply #8 on: July 17, 2017, 05:01:56 pm »
thanks , i'll take a look ;)

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: smallest fpc program size
« Reply #9 on: July 17, 2017, 05:11:21 pm »
- embedded: you can count in bytes, not Kb, to turn e.g. a led on/off
Mentioning mingw and exe I don't think he was refering to target embedded  ;)

I know... Did you grasp what I wrote? But loose the weight of all I/O etc. and it just needs startup code. System.pas is way overblown. You can strip-out 95% (e.g. KOL's sysdcu for Delphi) of it when needed. You'll end up with about the same size as a C program that doesn't use any libraries: bytes, well within a KiloByte. But you need skills to do so. NativeNT will not help very much: it is basically Windows with ~5% of the code linked in.

And most definitely will lead to more questions about why things do not work (yes, they stripped any convenience for a couple of bytes and then want it back!)..... Especially Pascal programmers do not compare like for like... (and learn to compare their code to C code). FPC makes it actually possible to come very close to - or better - than C if you know WHY your minimal executable is larger than a minimal C program.... Not many people here, not even experienced ones on this forum, do..

Such questions always arize... They are always by people who do not understand what is happening. They are always by people who want the convenience of everything that is in system.pas but do not want the ballast... You can hear them squeel... >:D >:(
« Last Edit: July 17, 2017, 05:19:51 pm by Thaddy »
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: smallest fpc program size
« Reply #10 on: July 17, 2017, 05:17:18 pm »
WHY your minimal executable is larger than a minimal C program.... Not many people here, not even experienced ones on this forum, do..

So, you are happy with the Size Matters faq now ?  >:D

I'll give the url since it is appropriate: http://wiki.freepascal.org/Size_Matters

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: smallest fpc program size
« Reply #11 on: July 17, 2017, 05:20:49 pm »
Ahum. You are one of those that do know... :-* :-X

I can just help by writing the same faq in a more offensive >:( ;D :D way...
« Last Edit: July 17, 2017, 05:22:43 pm by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018