views:

140

answers:

6

Hello, I am interested in reducing the file size of my application. It is a MFC/C++ application built with MVC++ in Visual Studio 2008. UPX does a good job of reducing the final exe to about 40% of its original size but I would like to reduce it more.

MFC must be statically linked in this project.

I have tried some methods outlined in this question: reduce-windows-executable-size. Specifically applying different settings to the compiler/linker.

I believe i can reduce the size further by having a look at the 'cost' of including certain headers in the project.

Any tip on how to go about this, maybe a tool which could analyse my code for me? Thanks

+8  A: 

You are probably wrong in this. Removing headers can result in somewhat shorter build times, but as what they contain is mostly declarations (which you will need at some point anyway) they should have little or no effect on the size of the final executable.

anon
(+1) this is the correct answer.
Hassan Syed
+2  A: 

The only way to reduce the size of the project is to reduce the amount of code. As Neil stated, removing headers will only shorten the build time. Compilers will not include everything when doing a "using namespace" clause, they'll only select that which is needed. Now, on the other hand, if you're adding headers to things not used anywhere at all within the project that is a good indication of a class that should be removed.

wheaties
+2  A: 

Your assumption apparently is that executable size is somehow the sum of contributing components, and in particular source files. It just doesn't work that way.

For instance, assume the code for std::list<T>::size. It may be used in many Translation Units. Yet, the linker will fold many copies together, sometimes even for different types T. But how would you account for the resulting bytes in the exectuable?

Now, if you can't even determine how to account for the bytes of that simple (set of) functions, then how would you account for even more complex constructs? And if you can't allocate bytes used in the executable to indivual source files, then you can't determine individual contributions.

MSalters
+2  A: 

Headers typically only contain:

  1. Macros
  2. Type definitions (like classes)
  3. Function forward declaration (the function definition is in the .c / .cpp file)

None of the above actually results in machine code being generated unless they are actually used by the code in your .c /.cpp files.

Now all of the above needs to be parsed (which adds to compile time) but will be ignored unless actually used.

doron
+1  A: 

Most of the existing answers have assumed that headers only contain declarations, which have no impact on executable size. Of course that's true if that assumption holds, but it is getting fairly common for headers to contain actual code as well (function definitions, typically), and those do contribute to code size.

On the other hand, they only contribute if those functions are actually linked into the final executable. And they are only linked into the final executable if you call those functions. So even though the header might contribute to growing your executable size, what are you going to do about it? You can't remove the code that you use. Removing the header file just isn't an option then -- unless you move that code somewhere else, and then it'll increase the executable size anyway.

So either a header makes no difference, or it does make a difference because you use the code that's in it, and then it can't be removed. In either case, removing headers isn't going to buy you much.

jalf
A: 

Some ideas for shrinking code size:

  1. Move constant data into a resource file or other text file.
  2. Move configurable constants into a text file.
Thomas Matthews