Solved

Smaller executable

Posted on 2004-03-30
23
299 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 500 total points
ID: 10720857
Here is some material explaining that

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

Amit
0
 
LVL 30

Expert Comment

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

Expert Comment

by:Sys_Prog
ID: 10721223
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now