Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

Visual C++: How to Create Standalone Win32 Application Not Requiring Visual C++ Runtime

3.91/5 (12 votes)
9 Dec 2014CPOL2 min read 88.7K   641  
How to statically link a Win32 Project with Visual C++ libraries (m***.dll)

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:

Image 1

If you will open your EXE in DependencyWalker, you will see it requires one or few DLLs but Win32 DLLs (user32, kernel32, etc)!

Image 2

What can we do to make the project standalone?

  1. We can put all of the required DLLs to folder with EXE or to C:\Windows\system32\ path.
  2. 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.
  3. 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

  1. First, go to Project -> Properties menu.
  2. Go to Configuration Properties -> C/C++ -> Code Generation.
  3. Set Runtime library property to Multi-threaded (/MT) or Multi-threaded Debug (/MTd).
  4. Build and debug it!

Statically Linking - Way 2

  1. First, go to Project -> Properties menu.
  2. Go to Configuration Properties -> Linker -> Command Line. Copy all lib names to the clipboard:

    Image 3

  3. Go to Configuration Properties -> General. Set Use of MFC property to Use MFC in a Static Library.
  4. 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:

    Image 4

  5. Build and debug it!

Result

Now your EXE weighs a few hundred KBs more than before, but, it is fully standalone!

Image 5

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)