Link to home
Start Free TrialLog in
Avatar of dankennedy
dankennedy

asked on

Smaller executable

I'm writing a command line c++ program and I'm using the Borland free command line compiler. How can I get the size of my program down? It's really not doing a whole lot, but it's already up to 250KB. Is there something I can do differently in terms of include files or with variable decalaration to make the size of my executable smaller?

Thanks in advance!
Avatar of Axter
Axter
Flag of United States of America image

What type of program is it?  WIN32, DOS Console, ANSI?

Your compiler should have an option for compact compile.
With out knowing more about your application, it's difficult to advise you.

Please provide more details.
ASKER CERTIFIED SOLUTION
Avatar of Sys_Prog
Sys_Prog
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nice link Sys_Prog
Hi Sys_Prog,
http://www.catch22.org.uk/tuts/minexe.asp
That's an interesting page.

Cheers,
Stefan
Yep

I remember having a good link for implementing your own RunTime Function and reducing the executable size by eliminating the library code
But not able to find it now
Anyway, I am searching for it on my disk and would post it if I am successful finding it

Amit
Avatar of nonubik
nonubik

Maybe it's compiled in debug. In release mode should have a smaller size...
nonubik,
Not necessarily. Code can get very bloated just by using a couple of STL classes.

Stefan
May be off topic, but in any case You can pack it with upx.
mokule,
Yep, upx is nice.

Stefan
...provided you only expect to execute one instance at a time.

http://www.catch22.org.uk/tuts/minexe.asp:

> The disadvantage of this technique is that multiple copies of your program will not share the same memory location, so more RAM is required when running multiple instances.
> How can I get the size of my program down?

Exactly what problem are you trying to solve? Why is a 250K executable causing you problems, as opposed to a 200K exe or 100K exe?

When you can buy 100 Gb+ drives for < $100, I'm not sure it makes sense to spend any time at all trying to squeeze a few kilobytes out of a file. You could be spending this time adding more features to your program. :)

I'd worry more about how much memory the program will use once it's running, this will have a far more noticeable impact than how big it is on the disk.

There is a certain aesthetic I guess in having a small compact program, but I wonder if the benefit is really worth the pain of removing all the CRT calls from your code or writing your own startup function or whatever.
Avatar of dankennedy

ASKER

Thanks for all the good links! Let me take a look at them now...

The reason I need a smaller program is because the app I'm trying to write is going to be deployed over the network to MANY computers.  A smaller executable would speed up the deployment process significantly.  I'm sure a program which does what mine does, could be compacted down to least 100K. It's a console app and will run as an NT service.

I'll check out the links you guys posted and get back to you.

Thanks!
In case You want to look at upx here is a link
http://upx.sourceforge.net
> The reason I need a smaller program is because the app I'm trying to write is
> going to be deployed over the network to MANY computers.

Hmmm - 100Kb smaller * 1000 machines = 100 Megabytes. On a 10 Mbit/second network you save around 100 seconds...

In any case, the best bang for your time IMO is probably to use image compression, you can add it right into your build procedure. UPX is ok, although last time I tried it it did not work with all the exe's I tested it with. I've used one called AsPack in the past which works great, but it it not free.
> The reason I need a smaller program is because the app I'm trying to write is
> going to be deployed over the network to MANY computers.

Hmmm - 100Kb smaller * 1000 machines = 100 Megabytes. On a 10 Mbit/second network you save around 100 seconds...

In any case, the best bang for your time IMO is probably to use image compression, you can add it right into your build procedure. UPX is ok, although last time I tried it it did not work with all the exe's I tested it with. I've used one called AsPack in the past which works great, but it it not free.
While not exactly C++ and also related to Linux/unix etc

The following link might be an interesting read for some of you

http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html

A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux

-Abhijit

> http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html

I like that, but here's a 4 byte executable that does the same thing in DOS:

(1) Source code:

debug.scr
--------8<--------
a
mov ax,42
ret

n42.com
r cx
4
w
q

--------8<--------

(2) Assembly creates a file called 42.com:

    debug < debug.scr

There, now, wasn't that useful? :-)
NB: You need that blank line at the end of debug.scr.
What a shame, just checked this and my memory is wrong. Looks like returning with the error level in AL doesn't do the trick. You need it to be:

debug.scr
--------8<--------
a
mov ax,4c42
int 21

n42.com
r cx
5
w
q

--------8<--------

That returns 0x42 as the error level. Sadly that's 5 bytes rather than 4. Code bloat!
Sorry for the delay... Thanks for the help.