Thomas Konstantin
Thomas Konstantin

Reputation: 29

How to run a c program i wrote on another computer

I wrote a program and compiled it with Visual Studio 2017. The .exe file runs with no problem on my PC, the one on which it was compiled, but when I take the .exe file, or even the whole folder to a different machine, it won't work.

First of all, I tried coping the whole folder including the .c and the .h files; also, I tried downloading an extension for Visual Studio which allows me to create a .msi installation file, which also didn't change much.

The error messages I get are the following ones:

the code execution cannot proceed because ucrtbased.dll was not found
this code execution cannot proceed because vcruntime140.dll was not found

From research I did, the main reason for the problem is the lack of appropriate c++ redistributable files. My main problem here is that this program was written for a small company, and its problematic for me to make them install additional software. Is there any way of solving this without making them install additional software? Can i somehow fix this by including more files with the .exe file?

Upvotes: 1

Views: 3669

Answers (2)

Volv1t3
Volv1t3

Reputation: 1

I had the same issue just today and couldn't come up with a solution that was a) not overly complex as my level in C++ is just above beginners, b) platform independent, and c) wholly portable and professional looking.

But, after careful googling and scrolling along forums and Stack Overflow, I think I found the best way to ship a C++ or C# application in a standalone format, capable of running along in someone else's computer without the need to have a complicated environment or asking anyone to install software and plugins from the web.

By the way, I'm mostly based on this YouTuber's video YOUTUBE TUTORIAL so If I make it sound too confusing just go look at that video and you'll get through this.

Basically, as Adrian Mole mentioned, you need to pack your stuff into a .MSI file so that you can output a program based on your C#/++ application, in my case I wanted a console application so what I did was:

  1. Open up A new project in Visual Studio 2022

  2. Create your application, console, or whatever you might need (in my case I ported my files from another directory in my PC, I was using Clion before)

  3. Go to Extensions -> Search -> Visual Studio Installer Projects, go ahead and install it and restart visual studio.

  4. In your solution folder, create another project, just that this time create a Setup Project (new project type added in by the extension). In here you will see three folders Application, User's Desktop, User's Programs Menu

  5. In here please select your project name and right-click it, there should be a properties label in the drop-down menu that appeared.

  6. In this Properties Menu, look down to the label called prerequisites, this are basically plugins like .NET or Visual C++. In here select any files that your program requires to run (In my case, for a C++ console application I used .NET Runtime 6.0.18 (x64) and Microsoft Visual C++ 14.

  7. After selecting all of this, go back out and select Configuration Manager in the top right corner of your screen. Here I made the following adjustments: swapped debug version for the release version in my Setup Project and the same for my real project

  8. Once done with all that go back out and let's get ready to connect the files.

  9. On the right side of your screen there are two white sections, one with the folder names and another with an empty space, here you will add your project. First double-click on the "Application Folder". Then, right-click on the empty portion of the screen and select add->Project Output, and your project will be automatically loaded (this happened to me), just click ok and it will appear in the folder.

  10. Then I would suggest you right-click on the item that appeared now and create two shortcuts for these files and rename them whatever you want

  11. If you are like me and want it to look pretty (even for a console application) what you can get is a .ICO file from the internet and put it in the main Application Directory. Here just right-click on the shortcuts and select properties again, now where it says icon click on the little arrow on the side and select browse, look for the folder (in your Solution) where you put your icon and that's it for this section

  12. Finally, to push this out and make the .MSI file, you **right-click on the Setup project you created -> click build -> FINAL RESULT! **. It will take some time depending on the number of files you have to loop through but in the end, you should have a folder that says release and you will find a .MSI file waiting for you to deliver it to your customers.

I hope this helps, I know Im super late to answer this but, you never know, someone else might need to hear this, anyways that's all I have for this solution, there's probably more you can change in these setup project so definitely check out the channel I mentioned.

Upvotes: 0

Adrian Mole
Adrian Mole

Reputation: 51874

Almost all C or C++ programs built with Visual Studio 2015 or later will only run (properly) on PCs that have the relevant VC Redistributable installed.

You say you don't want folks to have to install something else - but, one way or another, they will have to. However, as you are building an install package, you can probably set the relevant redistributable as a "prerequisite" in that installer.

This will mean creating a "Setup.exe" file to run the .msi package, though. To use this method, right click on your ".vdproj" project in the "Solution Explorer" and click the "Prerequisites" button. (If this button is not enabled, then be sure to select one of your "Release" configurations in the top-left of the property page.)

When you click this, make sure you check the "Create setup program to …" check-box at the top of the "Prerequisites" dialog, then scroll down and select the relevant "Visual C++ '14' Runtime Libraries" option (one for each supported platform). [Note, although they're called VC '14', they will each work for any VC version from 14 upwards!]

You will then have to distribute both the "Setup.exe" program and your .msi package. When they double-click "Setup.exe," that will check for and, if necessary, download and install the redistributable.

There is another way to do it, by including the relevant, platform-specific redistributable in your install package, then running that as a "Custom Action" from the installer. But this isn't trivial! I can post some sample code for doing this, if you elect to go down this road.

Upvotes: 2

Related Questions