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:
- Comment out calls/usage of these components.
- 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
- Is my assumption valid (
2
to give the same results as1
)? - 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?