2

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?

Dean Harding
  • 71,468
  • 13
  • 145
  • 180
Lienau
  • 1,353
  • 1
  • 20
  • 38
  • 3
    Why do you need to keep it below 20kB? – Matti Virkkunen May 16 '10 at 23:13
  • No, it's because is a class which requires runtime support. when you include that header than the compiler needs to include std::basic_string and friends into your module. – Billy ONeal May 16 '10 at 23:15
  • If you completely avoid C++ stuff and only go with C, it'll reduce your program's size somewhat while almost certainly increasing the complexity. Handling such complexity might in fact require more space than the resulting C++ code would. It's difficult to get away with <20KB sometimes these days...especially on Windows. For example, a simple bit of `#include int main (void) { return !puts ("Hello World!"); }` with MinGW (GCC on Windows) yields 8K alone and that's using native libraries. If I use the MinGW libraries (`-ansi`), it becomes >20K already. – Dustin May 16 '10 at 23:30
  • Using the `/MD` flag with Visual C++ 2008 Express to use the MSVCRT library rather than the LIBCMT library and then stripping it using `strip.exe` from MinGW yields 5K...and that's the smallest that I can get it. My point is that 20K is a bit unrealistic these days for Windows. I should also mention that you can use a utility called UPX to make it smaller, but that's at the expense of memory cost. Less cost to you in EXE size = more cost in memory. There's a trade-off somewhere that is unavoidable. Sorry. = / – Dustin May 16 '10 at 23:34
  • 1
    You *need* to stay below 20kb? I find that hard to believe. – jalf May 17 '10 at 01:30
  • @Dustin: You can actually get it down to something in the hundreds of bytes, but then you're starting to strip away compatibility layers. – L̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳ May 17 '10 at 20:56
  • @Longpoke: Exactly, and that could very well be a bad idea. I suppose it depends on how compatible the code should be... – Dustin May 17 '10 at 21:07

5 Answers5

10

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
  • 71,468
  • 13
  • 145
  • 180
  • 2
    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. – L̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳ May 16 '10 at 23:17
  • @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 May 16 '10 at 23:18
  • 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 May 16 '10 at 23:21
  • @Lineau: Windows.h and Wininet.h are C libraries. – Billy ONeal May 16 '10 at 23:23
  • 1
    Yes, the Windows API is pure C anyway, so you still have access to all of that. – Dean Harding May 16 '10 at 23:23
  • 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 May 16 '10 at 23:49
  • @Billy ONeal, yeah I meant virtual functions, my bad. – L̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳ May 17 '10 at 00:54
6

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
  • 278,309
  • 50
  • 514
  • 539
  • 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 May 16 '10 at 23:45
0

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
  • 58,701
  • 10
  • 113
  • 156
0

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
  • 339,232
  • 124
  • 596
  • 636
0

Remember to build your app on - release mode instead of -debug one. That will reduce your app size a lot.

Taryn
  • 242,637
  • 56
  • 362
  • 405
juan
  • 11