How to reduce the filesize of a compiled file?

General FreeBASIC programming questions.
mrminecrafttnt
Posts: 42
Joined: Feb 11, 2013 12:23

How to reduce the filesize of a compiled file?

Postby mrminecrafttnt » Aug 12, 2017 18:21

A simple compiled

Code: Select all

Print "Hello World"

have a filesize of 24,5kb.
Is it possible to reduce this??
marcov
Posts: 2347
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: How to reduce the filesize of a compiled file?

Postby marcov » Aug 12, 2017 20:19

sure, customize everything. Roll your own RTS and compiler.
PaulSquires
Posts: 586
Joined: Jul 14, 2005 23:41
Contact:

Re: How to reduce the filesize of a compiled file?

Postby PaulSquires » Aug 12, 2017 21:12

marcov wrote:sure, customize everything. Roll your own RTS and compiler.


LOL, that actually did make me laugh out load. Good one.
Tourist Trap
Posts: 2197
Joined: Jun 02, 2015 16:24

Re: How to reduce the filesize of a compiled file?

Postby Tourist Trap » Aug 13, 2017 11:25

mrminecrafttnt wrote:A simple compiled

Code: Select all

Print "Hello World"

have a filesize of 24,5kb.
Is it possible to reduce this??

Maybe you can try to compile your code to ASM (a switch exists for that when you call fbc) then tweak it by hand?
Source code
-b < name >
Add a source file to compilation
-i < name >
Add a path to search for include files
-include < name >
Include a header file on each source compiled
-d < name=val >
Add a preprocessor's define
-lang < name >
Select language mode: fb, fblite, qb, deprecated
-forcelang < name >
Select language mode: fb, fblite, qb, deprecated (overides statements in code)
Code generation
-target < platform >
Set the target platform for cross compilation
-gen < backend >
Sets the compiler backend (default is 'gas').
-asm < format >
Sets the assembler format for Asm block.
-arch < type >
Set target architecture (default: 486)
-O < level >
Set the optimization level (-gen gcc).
-vec < level >
Set level of vector optimizations enabled by the compiler (default: 0)
-fpu < type >
Set the floating point arithmetics unit (default: FPU)
-fpmode < type >
Select between fast and accurate floating-point operations (default: PRECISE)
-z < value >
Sets miscellaneous or experimental options.
Compilation
-m < name >
Main file without extension, the entry point (default is the first .bas file on the command line)
-g
Add debug info
-profile
Enable function profiling
-e
Add error checking
-ex
Add error checking with RESUME support
-exx
Same as -ex plus array bounds and null-pointer checking
-Wa < opt >
Pass options to GAS (separated by commas)
-Wc < opt >
Pass options to GCC (separated by commas)
-o < name >
Set object file path/name (must be passed after the .bas file)
Linking
-a < name >
Add an object file to linker's list
-l < name >
Add a library file to linker's list
-p < name >
Add a path to search for libraries
-mt
Link with thread-safe runtime library
-nodeflibs
Do not include the default libraries
-static
Prefer static libraries over dynamic ones when linking
-map < name >
Save the linking map to file name
-Wl < opt >
Pass options to LD (separated by commas)
-export
Export symbols for dynamic linkage
-lib
Create a static library
-dylib
Create a DLL, including the import library
-dll
Create a DLL, including the import library. (Same as -dylib)
-x < name >
Set executable/library path/name
Behaviour
-prefix < path >
Set the compiler prefix path
-version
Show compiler version on the command line, do not compile or link.
-v
Be verbose
-print < option >
Display certain information (host, target, etc.)
-pp
Emit the preprocessed input file only, do not compile
-r
Compile into intermediate file(s) only, do not assemble or link
-rr
Compile into asm file(s) only, do not assemble or link

-c
Compile and assemble source file only, do not link
-R
Do not delete the intermediate file(s)
-RR
Do not delete the asm file(s)
-C
Do not delete the object file(s)
-w < value >
Set min warning level: all, pedantic, next or a value
-maxerr < val >
Only stop parsing if <val> errors occurred
-noerrline
Do not show source line where error occurred
Target specific
-s < name >
Set subsystem (gui, console)
-t < value >
Set stack size in kbytes (default: 1M)
Meta
@< file >
Read (additional) command-line options from a file
srvaldez
Posts: 1354
Joined: Sep 25, 2005 21:54

Re: How to reduce the filesize of a compiled file?

Postby srvaldez » Aug 13, 2017 13:19

mrminecrafttnt wrote:have a filesize of 24,5kb.
Is it possible to reduce this??

yes (example from https://msdn.microsoft.com/en-us/library/y8b57x4b.aspx)

Code: Select all

#include "crt.bi"

dim as string frmt = !"%s %s\n"
dim as string hello = "Hello"
dim as string world = "world"
asm
      mov  eax, [world]
      push eax 
      mov  eax, [hello]
      push eax 
      mov  eax, [frmt] 
      push eax 
      call printf 
      'clean up the stack so that main can exit cleanly 
      'use the unused register ebx to do the cleanup 
      pop  ebx 
      pop  ebx 
      pop  ebx
end asm

exe size 19kb
or just use upx https://upx.github.io
upx --brute hello.exe
exe size 12k
dodicat
Posts: 4338
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to reduce the filesize of a compiled file?

Postby dodicat » Aug 13, 2017 13:58

This one shows 17kb on Win 10.
-gen gcc -s console
on the 32 bit compiler.

Code: Select all

#include "crt/io.bi"
#include "crt/stdlib.bi"
printf(!"Hello World!\n")
system_("pause")

 
marcov
Posts: 2347
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: How to reduce the filesize of a compiled file?

Postby marcov » Aug 13, 2017 16:55

PaulSquires wrote:
marcov wrote:sure, customize everything. Roll your own RTS and compiler.


LOL, that actually did make me laugh out load. Good one.


It was not meant as a joke. If you have extreme views on topics like minimal size, I don't see why the general distribution should cater for this.

At freepascal, we also get this question ad nauseam, which is why there is a Faq about it. Feel free to borrow text from it, I mostly wrote it.
UEZ
Posts: 42
Joined: May 05, 2017 19:59
Location: Germany

Re: How to reduce the filesize of a compiled file?

Postby UEZ » Aug 13, 2017 17:58

Following the instruction from here: http://www.dbfinteractive.com/forum/ind ... 10.30;wap2

Exe size = 2kb
fxm
Posts: 7272
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: How to reduce the filesize of a compiled file?

Postby fxm » Aug 13, 2017 18:11

For fbc rev 0.18.3 or older !
UEZ
Posts: 42
Joined: May 05, 2017 19:59
Location: Germany

Re: How to reduce the filesize of a compiled file?

Postby UEZ » Aug 13, 2017 21:32

It works for me with the latest version (1.05). Of course the 2kb is only for this simple example!

It would be great to have a feature to remove all unneeded function before compiling to reduce the size.

Apropos size, imho only for the 4/8/64 kb demo competitions size matters. ;-) Any example how to use Crinkler with FB?
jj2007
Posts: 50
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: How to reduce the filesize of a compiled file?

Postby jj2007 » Aug 13, 2017 22:40

Rumours say a standalone "hello world" in QT is around 10MB. Why are you worried about 20k?
If a language has many features, there is always a certain overhead. The more interesting question is the size of a big source, say: 10,000 lines.
caseih
Posts: 1009
Joined: Feb 26, 2007 5:32

Re: How to reduce the filesize of a compiled file?

Postby caseih » Aug 14, 2017 1:32

So basically the 24k size of the simple hello world program is mostly the size of the necessary parts of the FB runtime statically-linked in. I'd say that's pretty efficient and quite cheap!

Actually Qt-linked executables are no bigger or smaller than any others. However they do dynamically link against the Qt libraries, which are about 10 MB in size. Now theoretically you only pay for this once, as multiple executables can used the same shared libraries. This is true for disk space and also memory space, as multiple processes can use the same shared library. On Windows, however, the lack of a good DLL installation and versioning scheme (such as MacOS's frameworks) basically means each Qt app has its own set of libraries (and thus don't share memory either).

In any case, there's so much more to the story than executable size. You need to look at what the other dependencies are as well, which vary by operating system. In linux's case there's the obvious libc dependency, as well as ncurses. Also, even the simplest FB program needs a part of the FB runtime library to be statically linked into the executable. I'm not sure what granularity the linker works on, whether it can just bring in the routines used (the print statement, for example) or what.

The trick UEZ mentions basically involves not using any part of the FB runtime library at all, but just using OS API calls. Sort of useful I guess, if you don't plan to use any form of strings or arrays. Basically you're doing raw C programming, but in an FB flavor. Except that you can't use C runtime calls either, so no C-style strings, which makes even working with the win32 api difficult.

I can't think of too many reasons to do all this, though, except for maybe embedded programming like an Arduino. Though even there, judicious use of the C and FB runtimes (and only linking in those parts absolutely needed) would still be quite useful.
fxm
Posts: 7272
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: How to reduce the filesize of a compiled file?

Postby fxm » Aug 14, 2017 5:04

UEZ wrote:It works for me with the latest version (1.05).

Where do you find the file "i386pe.x" as required at http://www.dbfinteractive.com/forum/ind ... 10.30;wap2 ?
Is it now useless (with fbc revision > 0.18.3) ?
UEZ
Posts: 42
Joined: May 05, 2017 19:59
Location: Germany

Re: How to reduce the filesize of a compiled file?

Postby UEZ » Aug 14, 2017 7:53

Just download and try it from here: http://www.mediafire.com/file/jnp7luk77 ... TinyFB.zip
mrminecrafttnt
Posts: 42
Joined: Feb 11, 2013 12:23

Re: How to reduce the filesize of a compiled file?

Postby mrminecrafttnt » Aug 14, 2017 8:20

I need this for the demoscene, so will now try fbc 0.18.6 :)
Edit: Nice!

Code: Select all

#include "crt/io.bi"
#include "crt/stdlib.bi"
printf(!"Hello World!\n")
system_("pause")

Needs only 6kb with fbc 0.18.6 this is what i need! Thanks!
Now is it time to make an 8k demo :)

Return to “General”

Who is online

Users browsing this forum: No registered users and 4 guests