3

When I compile a simple program:

#include <iostream>

using namespace std;
void main() {
    cout << "Hello world!";
}

And tun the compiled .exe on another system without visual studio installed I receive the following error:

The Code execution cannot proceed because VCRUNTIME140.dll was not found. Reinstalling the program may fix the problem.

When I compile with cl.exe I receive no errors, does anyone know a workaround to this without installing VCRUNTIME140.dll on the systems. (I've tested on multiple windows systems including a windows virtual machine)

cdev
  • 31
  • 1
  • 2
  • 4
    You need to install the C++ runtime libraries. Normally VS comes with the redistributable installers, but you can also download from their website. [Download link](https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads). It is required, since any C or C++ program compiled with VS depends on them. Alternatively, you just copy all the required files over too. – ChrisMM Dec 27 '19 at 00:50

3 Answers3

7

I've encountered this problem before and there's a simple solution to it,

The missing .dll are a issue of static linking not missing packages (in most cases), becuase visual studio 2019 comes pre-installed with what you need.

To fix: go to your project properties (in project tab) Select C/C++ Change the value of runtime library to "Multi-threaded debug (/MTd)"

This will cause the compiler to embed the runtime into the app. The executable will be significantly bigger, but it will run without any need of runtime dlls.

asd plougry
  • 143
  • 1
  • 9
  • 1
    What you are describing is called "static linking" with the C++ runtime. I forgot to include that option in my own answer. But to amend your proposal, I would suggest only the Debug builds should be linked with `/MTd`. Release builds should be linked with the `/MT` (Mutli-threaded). – selbie Dec 27 '19 at 04:18
  • And welcome to Stack Overflow. Here's your first upvote of hopefully many more to come. – selbie Dec 27 '19 at 04:18
  • What about with python selbie? https://stackoverflow.com/questions/59496003/python37-dll-not-linked-in-executable – asd plougry Dec 27 '19 at 06:01
  • That's an entirely different question. – selbie Dec 27 '19 at 08:14
  • 1
    But I answered it anyway because I'm a nice guy. :) – selbie Dec 27 '19 at 08:18
  • This will also cause the CRT to start leaking resources. It seems you aren't aware of the consequences of statically linking against the runtime library. – IInspectable Dec 28 '19 at 23:35
2

Get the "Visual Studio 20xx VC++ Redistributable package" for your version of Visual Studio. Then run on the target machine to install.

Bottom of this page: https://visualstudio.microsoft.com/downloads/

enter image description here

Or bottom of this page for older versions of Visual Studio: https://visualstudio.microsoft.com/vs/older-downloads/

enter image description here

selbie
  • 100,020
  • 15
  • 103
  • 173
2

I've had the same problem, mainly because originally when compiling something with C++ and turning it into an exe file, it's still gonna be an exe file that depends on libraries from C++.

But according to asd plourgy, who had a good idea to change the value of the runtime library, I wanted to share with whoever seeks knowledge how I solved it:

Go to your Visual Studio Code and follow these steps:

  1. Click on Project
  2. Properties
  3. Scroll out C/C++
  4. All Options
  5. runtime library
  6. Change value to: "Multithreaded-DLL (/MD)".

And that should do the trick. Afterwards, you have to obviously

  1. save
  2. debug
  3. create new(exe)
  4. open cmd and run the exe to make sure it works.

My System is: Windows 10

Here are a few pictures to make the steps easier, it's in german though:
step1:
step1

step2:
step2

step3:
step3

step4:
step4

step5:
step5

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Kapejko
  • 21
  • 1