?
Solved

Smaller executable

Posted on 2004-03-30
23
Medium Priority
?
328 Views
Last Modified: 2010-04-01
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!
0
Comment
Question by:dankennedy
  • 5
  • 3
  • 3
  • +6
23 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 10720854
What type of program is it?  WIN32, DOS Console, ANSI?

Your compiler should have an option for compact compile.
0
 
LVL 30

Expert Comment

by:Axter
ID: 10720856
With out knowing more about your application, it's difficult to advise you.

Please provide more details.
0
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 2000 total points
ID: 10720857
Here is some material explaining that

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

Amit
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 30

Expert Comment

by:Axter
ID: 10720877
Nice link Sys_Prog
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 10721332
Here's the original MSJ article http://www.microsoft.com/msj/archive/S569.aspx
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10721566
Hi Sys_Prog,
http://www.catch22.org.uk/tuts/minexe.asp
That's an interesting page.

Cheers,
Stefan
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10721591
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
0
 
LVL 16

Expert Comment

by:nonubik
ID: 10721815
Maybe it's compiled in debug. In release mode should have a smaller size...
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10722637
nonubik,
Not necessarily. Code can get very bloated just by using a couple of STL classes.

Stefan
0
 
LVL 17

Expert Comment

by:mokule
ID: 10722879
May be off topic, but in any case You can pack it with upx.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10723181
mokule,
Yep, upx is nice.

Stefan
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 10723305
...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.
0
 
LVL 14

Expert Comment

by:wayside
ID: 10723812
> 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.
0
 
LVL 2

Author Comment

by:dankennedy
ID: 10724285
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!
0
 
LVL 17

Expert Comment

by:mokule
ID: 10724465
In case You want to look at upx here is a link
http://upx.sourceforge.net
0
 
LVL 14

Expert Comment

by:wayside
ID: 10724530
> 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.
0
 
LVL 14

Expert Comment

by:wayside
ID: 10724531
> 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.
0
 
LVL 11

Expert Comment

by:avizit
ID: 10741414
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

0
 
LVL 17

Expert Comment

by:rstaveley
ID: 10742149
> 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? :-)
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 10742156
NB: You need that blank line at the end of debug.scr.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 10746542
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!
0
 
LVL 2

Author Comment

by:dankennedy
ID: 10754879
Sorry for the delay... Thanks for the help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question