There is an excellent article written about how to reduce EXE/DLL size, take a look at
here.
The article was written a long time ago, but most of the points in that article are still applicable.
Here are the steps to create small EXE/DLL in Visual Studio 2019.
- Open Visual Studio 2019
- File -> New -> Project
- Type in "Windows Desktop Application" in the search box (or "Dynamic-Link Library (DLL)" for creating DLL)
- View -> Solution Explorer
- Delete all the files under "Header Files" and "Resource Files"
- Project -> Properties
- Configuration: Release
- C/C++ -> Code Generation -> Security Check -> Disable Security Check (/GS-)
- C/C++ -> General -> SDL Checks -> No (/sdl-) DLL only!!
- C/C++ -> Precompiled Header -> Precompiled Header -> (Not Using Precompiled Headers) DLL only!!
- Linker -> Input -> Ignore All Default Libraries (Yes /NODEFAULTLIB)
- Linker -> Manifest File -> Generate Manifest -> No (/MANIFEST:NO)
- Linker -> Advanced -> Randomized Base Address -> No (/DYNAMICBASE:NO) EXE only!! You are giving up the
ASLR feature.
Under Solution Explorer, you should have a file called WindowsProject1.cpp (for EXE) or dllmain.cpp (for DLL). If you see a pch.cpp, remove it.
// For EXE, replace the content of WindowsProject1.cpp with the following
#include <windows.h>
#ifdef _DEBUG
int WINAPI WinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPSTR lpCmdLine, _In_ int nCmdShow)
#else
int WINAPI WinMainCRTStartup(void)
#endif
{
OutputDebugString(TEXT("Hello"));
ExitProcess(0);
return 0;
}
// For DLL, replace the content of dllmain.cpp with the following
#include <windows.h>
#pragma comment(linker,"/export:Hello=_Hello@0")
EXTERN_C void WINAPI Hello()
{
OutputDebugString(TEXT("Hello"));
}
#ifdef _DEBUG
EXTERN_C BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
#else
EXTERN_C BOOL WINAPI _DllMainCRTStartup(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
#endif
{
return TRUE;
}