views:

200

answers:

5

I'm using Code::Blocks to write my program and when I include <string> (or <iostream>) the size of my exe grows. My program is very simple and I need to keep it small <20kb. I'm pretty sure this is happening because of the C++ Standards Committee swapped the old .h versions for many new libraries without the .h. But how would I keep it from adding the ~43kb? Are there settings for Code::Blocks so that it wont add the extra kb or is there another native lib I can use?

+10  A: 

If size is your #1 concern (and if you have to keep things < 20KB, then it probably is) then the standard C++ library is probably not the way to go. In fact, any C++ at all (RTTI, exceptions, etc) is probably a bad idea and you would be better off just sticking with straight C.

Dean Harding
I'd agree with you on the point of libraries, the C++ language itself isn't entirely expensive. For instance if you only stick to basic classes where you'd use structs, and use virtual inheritance only where you'd use a function pointer, it should be fine.
Longpoke
@Longpoke: Virtual inheritance should only be used in a few special cases where Multiple Inheritance is involved, where you inherit from two classes which share a common base class, and you only want one copy of the base class in that derived class. It has nothing to do with function pointers. If you mean virtual *functions*, however, that's a different story...
Billy ONeal
If I used C, would I still have access to windows.h, wininet.h just as I do now or would I have to use different stuff?
Lienau
@Lineau: Windows.h and Wininet.h are C libraries.
Billy ONeal
Yes, the Windows API is pure C anyway, so you still have access to all of that.
Dean Harding
I only started teaching myself C++ about a month and a half ago (I have experience with C# and PHP) and I don't know C. Now that I have to use all of the C functions I might as well just program in C. Thanks pointing that out.
Lienau
@Billy ONeal, yeah I meant virtual functions, my bad.
Longpoke
A: 

The header versions without .h aren't different from the others. The .h versions are mostly provided for compability. The additional size would be the string class, and the size isn't too surprising. If you wan't zero size overhead: Don't use std::string, but char*.

The next step would be compiler options. Most compilers have the possibility to disable RTTI and to optimize for size. -Os -fno-rtti would be the right switches for gcc.

Otherwise all standard tricks for size optimization apply:

  • Don't have any static data, always calculate
  • Don't use virtual functions
  • Pack things tight, use bitfields
  • Maybe C is a better pick than C++ (This is subject to debate.)
pmr
That only knocked out .5kb :/
Lienau
You mean virtual *functions*, not virtual inheritance. Virtual inheritance does not significantly affect code size.
Billy ONeal
@Billy Thanks, I got sloppy.
pmr
A: 

You could try using compiler switches to optimize based on file size. If you don't get anywhere you could try using stlport but I'm not sure if the results will be any better.

Brian R. Bondy
+6  A: 

Neither <string> nor <iostream> are renamed/modified C headers. They are both new to C++. If you want to stick to C libraries, you can use <cstring> and <cstdio> (among others), which are the C++ versions of the C headers "string.h" and "stdio.h".

Matthew Flaschen
This is really what I'm looking for. While just strait C is really the best solution, this is what I have to do to fix my immediate problem.
Lienau
A: 

remenber to build your app on - release mode instead of -debug one. that will reduce ur app size a lot.

juan
I'm already aware of that. But thanks anyway.
Lienau
-1 for being too lazy to type out the **word** "your" and for not using the shift key.
Billy ONeal
+1 to offset the pedantry of the commenter above me
advs89
@advs889: +1 to your comment for being able to type a standard English sentence. Also, you aren't a C++ programmer if you aren't pedantic ;)
Billy ONeal
@Billy ONeal:+1 for the typo in advs89's nick. Goes well with your comment about pedantry :).
slacker
@Slacker: Doh! (asdfasdfasdf 15 chars)
Billy ONeal