What decides the size of my executable c++ program file?? Apparently it does not depend only on the size/number of variables. How can i reduce the size keeping the number of variables constant?
Asked
Active
Viewed 2,384 times
1
-
1Build without debug information, build with optimizations for size, write less code. – Some programmer dude Oct 16 '12 at 18:21
-
1Apparently the most significant factor is the number of bytes the compiler writes out to the resulting file. – Oct 16 '12 at 18:23
-
Basically what you're asking is: "What does my binary executable actually contain?" – tenfour Oct 16 '12 at 18:25
4 Answers
4
There are many factors.
- Optimization by the compiler
- Static / Dynamic linking
- Static allocated memory
- Debug informations
- Stripped / Non-Stripped binaries
With gcc
you could use -Os
to optimize the result for size, but this may have an impact on the performance.

Thomas Berger
- 1,860
- 13
- 26
2
- Dynamically link to the C++ runtime.
- Compile the executable without debugging information.
- Compile with 'optimize for size' flags.
- Remove #iostream and fstream if you can, and use low level instead
- Also, using a lot of templates can cause size increase as well
- Use of lot of inline functions can cause increase in size.
- If you have lot of static and global variables then try to minimize them if possible.

VendettaDroid
- 3,131
- 2
- 28
- 41
1
In addition to the other answers, one other thing to keep in mind is inlining. Excessive inlining of functions can dramatically increase executable size.

WeirdlyCheezy
- 704
- 4
- 4
1
Generally, the size of your executable is not greatly based on how many variable constants you have. Based on the different OS that you are talking about, these are some aspects that can affect your executable size:
- Resources & libraries statically linked into the executable.
- Compiler options that you choose to generate your binary.
- Symbol files and other debugging util that might not have been stripped out from the executable.
This answer seems quite relevant :) Process for reducing the size of a executable