Page 1 of 1
How to make c++ programs smaller?
Posted: Sun Mar 08, 2009 6:35 pm
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?
Re: How to make c++ programs smaller?
Posted: Sun Mar 08, 2009 6:48 pm
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.
Re: How to make c++ programs smaller?
Posted: Sun Mar 08, 2009 7:00 pm
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?
Re: How to make c++ programs smaller?
Posted: Sun Mar 08, 2009 7:54 pm
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
Re: How to make c++ programs smaller?
Posted: Sun Mar 08, 2009 9:59 pm
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.
Re: How to make c++ programs smaller?
Posted: Sun Mar 08, 2009 11:00 pm
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.)
Re: How to make c++ programs smaller?
Posted: Mon Mar 09, 2009 12:00 am
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.
Re: How to make c++ programs smaller?
Posted: Mon Mar 09, 2009 12:10 am
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.
Re: How to make c++ programs smaller?
Posted: Mon Mar 09, 2009 3:10 pm
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
Re: How to make c++ programs smaller?
Posted: Mon Mar 09, 2009 3:35 pm
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.
Re: How to make c++ programs smaller?
Posted: Mon Mar 09, 2009 10:01 pm
by MarauderIIC
Makaze, you have the right of it. Debug is never built with optimizations. Release usually is.
Re: How to make c++ programs smaller?
Posted: Tue Mar 10, 2009 11:13 am
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.
Re: How to make c++ programs smaller?
Posted: Tue Mar 10, 2009 3:19 pm
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.
Re: How to make c++ programs smaller?
Posted: Tue Mar 10, 2009 5:51 pm
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..