Solved

Smaller executable

Posted on 2004-03-30
23
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

751 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