How to make c++ programs smaller?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
afaik
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 19
Joined: Mon Jun 16, 2008 11:13 pm

How to make c++ programs smaller?

Post by afaik »

I'm using Dev-cpp and it seems to me that the compiled programs are a bit on the large side for such basic instructions. I'm only using iostream and the executables are coming out to half a mB. Storage of the program isn't an issue with the new behemoth hdd's, but this would have been an issue 15 or 20 years ago if someone was writing in c++. Is there anything I can do from now (beginners level) to start being more efficient. Even if I compile with nothing in main but use the iostream header, the program still comes out to half a meg. Commenting out everything in main and the header file produces a 15kb exe. So what can I do to become more efficient?
User avatar
Kros
Chaos Rift Regular
Chaos Rift Regular
Posts: 136
Joined: Tue Feb 24, 2009 4:01 pm
Current Project: N/A
Favorite Gaming Platforms: PC, Playstation/2/3
Programming Language of Choice: C++
Location: Oregon,USA
Contact:

Re: How to make c++ programs smaller?

Post by Kros »

Learn ASM?

I think you're worrying about nothing at this point. Theres a pretty decent amount of code tied to those headers, thats why the file size is increasing. I really don't think its something you've done.
Isaac Asimov wrote:Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest.
YouTube Channel
afaik
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 19
Joined: Mon Jun 16, 2008 11:13 pm

Re: How to make c++ programs smaller?

Post by afaik »

That's the thing, I know it's not anything I've written. But is there a way to cut out any of the code of the iostream that I don't need to use? When I compile, is all the code of iostream being jumbled into my program, or only the parts that I need to use to get my program working?
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: How to make c++ programs smaller?

Post by programmerinprogress »

Mu current project doesn't use <iostream> and i'm compilling with a size of about 34 - 38kb(depending on my recent modifications)

*I'm talking about a fully blown SDL app aswell, I guess where the .exe size is small, the DLLs you put with it sort of cancels that out a little.

I don't really see your problem with a 15kb exe, just use memory sparingly if you want to cut down on wastage.

why is it much of an issue anyway?, we have plenty of memory these days ;)
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
CC Ricers
Chaos Rift Regular
Chaos Rift Regular
Posts: 120
Joined: Sat Jan 24, 2009 1:36 am
Location: Chicago, IL

Re: How to make c++ programs smaller?

Post by CC Ricers »

The compiler that you use and the flags you set also affect how efficiently your program is converted to binary code. I believe GCC/G++ in their default settings (at least in my experience) and MinGW produce larger executables for the same console program than the compiler built for Visual Studio. G++ does more static linking so file sizes are larger.
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: How to make c++ programs smaller?

Post by wtetzner »

afaik wrote:That's the thing, I know it's not anything I've written. But is there a way to cut out any of the code of the iostream that I don't need to use? When I compile, is all the code of iostream being jumbled into my program, or only the parts that I need to use to get my program working?
In Dev-C++, go to Project->Project Options. Go to the Compiler tab, and click on Linker on the left-hand side. In the drop down box next to "Strip Executable", select yes. See if that shrinks the executable at all. (Not sure how much it will help, but it's worth a try.)
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
afaik
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 19
Joined: Mon Jun 16, 2008 11:13 pm

Re: How to make c++ programs smaller?

Post by afaik »

why is it much of an issue anyway?, we have plenty of memory these days
I know, but if I ever do development on a target platform with limited memory, or on a console from the past 20 years (which I'd like to one day), I need to learn to keep the size down as much as possible.

Stripping the executable does improve file size, it cut's the executable from half to quarter of a meg. But what I'm still wondering is when programmers were programming 20 years ago with c++ on machines with less then a mB of ram, were they dealing with such large file programs for basic input/output? I got a hunch they probably weren't.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: How to make c++ programs smaller?

Post by avansc »

programmers from that era didnt use C++. used pascal, basic. cobal, fortran, etc. and ASM.
so dont worry about it.

if you ever are going to program for an environment with limited memory you would use different compilers and a compete different programming style. ex. if you were to program for PIC's you use a special compiler.
Last edited by avansc on Mon Mar 09, 2009 3:30 pm, edited 1 time in total.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: How to make c++ programs smaller?

Post by Ginto8 »

Just so you know, (from what I've heard) iostream adds a bit more overhead/executable size to the program. That may be the cause. ;)

just my 2 cents :)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
MakazeGamer
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Wed Oct 29, 2008 10:48 pm

Re: How to make c++ programs smaller?

Post by MakazeGamer »

Also, and this might be obvious but im just saying, that if you build it under the debug config it will be way bigger than if you build it under the release config. Now I have only used MCVS for the most part and I am not sure if other IDEs can change these configs or not.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: How to make c++ programs smaller?

Post by MarauderIIC »

Makaze, you have the right of it. Debug is never built with optimizations. Release usually is.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
thisisnotanick
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 11
Joined: Tue Mar 10, 2009 11:07 am

Re: How to make c++ programs smaller?

Post by thisisnotanick »

Hello! :)

This happens because of the way the Dev-CPP compiler (MingW) links the iostream lib. I have not found any way of getting around this :(
The good news is, the file size wont increase much if you use more from the lib.

File size on the same (very simple) project compiled with Dev-CPP vs. MSVC, is about 10x larger with Dev-CPP's compiler.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: How to make c++ programs smaller?

Post by MarauderIIC »

But you can actually release it by just copying the executable. So the increase in file size is probably something to do with the way it links the C library in general.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
thisisnotanick
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 11
Joined: Tue Mar 10, 2009 11:07 am

Re: How to make c++ programs smaller?

Post by thisisnotanick »

Yes, I think MinGW links statically. Not a problem really, like you said can just release the exe and be confident all it needs is there.
But kind of annoying when you just want some small progie to do some simple stuff, I end up compiling those in VC :) Gotta use it for something I guess..
Post Reply