The Problem
You may think that your Win32 project created in Visual C++ without MFC, does not require on target machine anything other than native WinAPI dlls.
But it's wrong!!!
You will see it if you will try to run the project, the creation of the Visual C++ 2010, on a freshly installed Windows:
If you will open your EXE in DependencyWalker, you will see it requires one or few DLLs but Win32 DLLs (user32
, kernel32
, etc)!
What can we do to make the project standalone?
- We can put all of the required DLLs to folder with EXE or to C:\Windows\system32\ path.
- We can install onto target machine a Visual C++ Redistributable with the same version as your Visual Studio and with the same bit count (x86 or x64) as your EXE.
- We can statically link these DLLs into your EXE - and get 100% standalone executable!
Remark: Information bellow applies to VC++ 2008 Express, but must work In other versions, too.
Statically Linking - Way 1
- First, go to Project -> Properties menu.
- Go to Configuration Properties -> C/C++ -> Code Generation.
- Set Runtime library property to Multi-threaded (/MT) or Multi-threaded Debug (/MTd).
- Build and debug it!
Statically Linking - Way 2
- First, go to Project -> Properties menu.
- Go to Configuration Properties -> Linker -> Command Line. Copy all lib names to the clipboard:
- Go to Configuration Properties -> General. Set Use of MFC property to Use MFC in a Static Library.
- Go back to linker command line.
You will not see these libs in linker command line! It's wrong because these DLLs are needed to run your app if it uses Win32 API. To correct it, add them to Additional options:
- Build and debug it!
Result
Now your EXE weighs a few hundred KBs more than before, but, it is fully standalone!
CLR (.NET)
Unfortunately, you can use static linking only for VC++ Redist and other unmanaged libs, but can't do it with managed libs, can't create CLR project not requiring .NET. Because .NET isn't just library kit, it's also interpreter-like compiler that runs your application and runs standard .NET DLLs (system.dll, etc.), it's needed to be installed on PC.