1

My goal is to reduce the binary size of an existing Visual Studio 2013 C++ project.

As I understood from my previous semi-related question, the Linker will include pieces of code, in the binary file, only if the code is used.

As part of my attempts to identify which components (classes/folders/filters) in my projects are the largest, I tried the following:

  1. Comment out calls/usage of these components.
  2. 1 + completely remove these components (files/folder) from my project.

I expected 2 to give the same results as 1 in terms of binary size. However found out that using 2 as much more impact on the size, meaning size reduction is much better.

Questions

  1. Is my assumption valid (2 to give the same results as 1)?
  2. If so, why don't I get the expected results?

Update 1

It seems like the Linker still includes code of unused functions, as if its logic tells it that it cannot exclude these functions since they might be used somehow (for example: exported functions) Just a reminder, the functions/classes I wish to exclude are not exported.

Example 1

void func(HINSTANCE, LPVOID lpReserved)
{
   // do nothing
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
   // do nothing
}

generates DLL of size ~300KB

Example 2

void func(HINSTANCE, LPVOID lpReserved)
{
   // do something huge
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
   // do nothing
}

generated DLL of size ~400KB

Question:

When generating DLL, if function is not used/called and it is not exported, why would the linker include its code?

I configured my Linker flags based on Do unused functions get optimized out?

Community
  • 1
  • 1
idanshmu
  • 5,061
  • 6
  • 46
  • 92

0 Answers0