Click here to Skip to main content
15,867,704 members
Articles / Web Development / HTML
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
3.91/5 (12 votes)
9 Dec 2014CPOL2 min read 85.8K   630   29   16
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)



Comments and Discussions

 
QuestionWorked as charm! Pin
Avtem3-Mar-21 19:13
Avtem3-Mar-21 19:13 
QuestionIt worked, thank you Pin
José Roberto Araújo Júnior9-Jul-16 10:50
José Roberto Araújo Júnior9-Jul-16 10:50 
GeneralMy vote of 1 Pin
Bartlomiej Filipek12-Dec-14 1:55
Bartlomiej Filipek12-Dec-14 1:55 
GeneralRe: My vote of 1 Pin
Emiliarge12-Dec-14 2:59
professionalEmiliarge12-Dec-14 2:59 
GeneralMy vote of 1 Pin
Ben Ratzlaff11-Dec-14 10:19
professionalBen Ratzlaff11-Dec-14 10:19 
GeneralRe: My vote of 1 Pin
Jamming111-Dec-14 10:43
Jamming111-Dec-14 10:43 
GeneralMy vote of 1 Pin
Jamming110-Dec-14 6:18
Jamming110-Dec-14 6:18 
QuestionIt's simple, why complicate things!!! Pin
Jamming110-Dec-14 4:53
Jamming110-Dec-14 4:53 
AnswerRe: It's simple, why complicate things!!! Pin
Emiliarge10-Dec-14 13:02
professionalEmiliarge10-Dec-14 13:02 
GeneralRe: It's simple, why complicate things!!! Pin
David A. Gray12-Dec-14 17:11
David A. Gray12-Dec-14 17:11 
QuestionOther / preferred(?) way Pin
_GyB_10-Dec-14 3:19
professional_GyB_10-Dec-14 3:19 
QuestionNow your EXE weighs a few hundred KBs more than before Pin
Kochise10-Dec-14 2:54
Kochise10-Dec-14 2:54 
AnswerRe: Now your EXE weighs a few hundred KBs more than before Pin
Emiliarge10-Dec-14 3:52
professionalEmiliarge10-Dec-14 3:52 
GeneralRe: Now your EXE weighs a few hundred KBs more than before Pin
Kochise29-Dec-14 1:33
Kochise29-Dec-14 1:33 
GeneralRe: Now your EXE weighs a few hundred KBs more than before Pin
Emiliarge29-Dec-14 5:59
professionalEmiliarge29-Dec-14 5:59 
GeneralRe: Now your EXE weighs a few hundred KBs more than before Pin
Kochise16-Jan-15 2:09
Kochise16-Jan-15 2:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.