1

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?

user1727119
  • 117
  • 2
  • 11

4 Answers4

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
  1. Dynamically link to the C++ runtime.
  2. Compile the executable without debugging information.
  3. Compile with 'optimize for size' flags.
  4. Remove #iostream and fstream if you can, and use low level instead
  5. Also, using a lot of templates can cause size increase as well
  6. Use of lot of inline functions can cause increase in size.
  7. 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:

  1. Resources & libraries statically linked into the executable.
  2. Compiler options that you choose to generate your binary.
  3. 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

Community
  • 1
  • 1
vgrimmer
  • 165
  • 2
  • 12