Create a self-contained program in C++ - c++

I'm using Visual Studio 2012. I wish to create a completely self-contained program that can be used on a machine without visual studio installed just by copying the exe file directly. I used to need to first run visual c++ 2012 redistributable to make the program work. Is there any way to make everything run in one exe? I would not like to use an installer either as it is a fairly simple program; the time it takes for the user to use the installer will probably be more than my program...
So is there any way to statically link everything? Right now, it just crashes when run on a new machine, but once I install VS2012 express on that machine, it suddenly starts to work.
Any help would be appreciated.

To get static linking in an MFC project set the project properties (for the release build) as follows:
C/C++ Code Generation, Runtime Library: Multi-threaded (/MT)
Configuration Properties, General: Use of MFC: Use MFC is a Static Library
A simple exe with these properties will run standalone.

Useful information found here http://msdn.microsoft.com/en-us/library/aa235516(v=vs.60).aspx
and here http://msdn.microsoft.com/en-us/library/f22wcbea.aspx

Once you run your program, you can find it's executable in the Debug\ folder of your project.
Hope it helps!
Sarge

Related

C++ program not running on windows systems without VS installed "VCRUNTIME140.dll was not found"

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)
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.
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/
Or bottom of this page for older versions of Visual Studio: https://visualstudio.microsoft.com/vs/older-downloads/
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:
Click on Project
Properties
Scroll out C/C++
All Options
runtime library
Change value to: "Multithreaded-DLL (/MD)".
And that should do the trick. Afterwards, you have to obviously
save
debug
create new(exe)
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:
step2:
step3:
step4:
step5:

Copy MFC Library to project

I am new to C++, and I have made a program on visual studio 2010. I then made an installer for it using Inno Setup. When I install it on my computer it runs fine, but when I move it to another computer and install it, an error appears like "mfc100ud.dll" is missing from your computer".
My question is: If I just copy the MFC files into my visual studio project directory, make another installer for it and run it on another computer; will the program work? Or do I have to do something else? Any help would be appreciated.
That 'd' in mfc100ud means you have linked to the debug version of MFC. The debug dll is only available with Visual C++ installed. You need to build a release version of your program. And you need to include the VC redistributable package in your installer, or change the runtime and MFC libraries to statically link into your program.

Problems with running EXE file built with Visual Studio on another computer

I created a client server application in C++ using Visual Studio.
Now I want to run the client EXE file on another computer (which doesn't have Visual Studio installed),
but when I try run the EXE file, it gives the following error message:
This application has failed to start because the application
configuration is incorrect. Reinstalling the application may fix this
problem.
How can I run the EXE file without installing anything on the computer?
Applications built with Visual Studio depend on Visual C++ Redistibutable (VCRedist). When the program is being linked dynamically, then your binaries will need
MSVCR**.dll (Microsoft C Runtime Library).
On MSDN, there is a nice article called Redistributing Visual C++ Files (for Visual Studio 2008), that states that there are Potential run-time errors in case that required Visual C++ library is not installed:
you may get one of the following error messages depending on the version of Windows on which you try to run your application:
The application failed to initialize properly (0xc0000135).
This application has failed to start because the application configuration is incorrect. Reinstalling application may fix this problem.
The system cannot execute the specified program.
Basically you have two options:
The simplest possible solution is to change the dynamic linking of runtime libraries to static linking. Go to project properties and under C/C++ → Code Generation you will find Runtime Library option. You need to change it from Multi-threaded DLL (/MD) to Multi-threaded (/MT).
Another possible solution is to make sure that the right version of Microsoft VC++ Redistributable Package is installed on the target machine.
But your application might depend on other DLL files as well. In case you want to find out what are the dependencies of your program, there is a great utility called Dependency Walker that will help you in this and many other situations :)
Background:
C++ applications need run-time assemblies (DLL files) to run in any Windows computer.
Normally these run-time assemblies are located at C:\Windows\Winsxs directory.
All the Windows operating systems by default comes with several run time assemblies.
But if your application is developed in a newer version of the run-time assembly environment, the target computer also needs the same version of the run time to exist there.
When you're installing Visual Studio, most newer versions of the run-time assemblies comes to your computer.
Solution:
Finally by anyway the target computer should have the exact run time assemblies. There are a few ways to do this (for more details search each in Google).
Statically link run-time assemblies with your application (troublesome for large application).
Install the C++ redistribution environment on the target computer (the easiest way).
Creating a setup project to deploy the run-time on the target computer when installing the application (not bad).
For deploying run-time assemblies as private assemblies (professional), see here for more details
Conditions:
You must not use .NET framework in your application.
You must not use the common language run-time support for your application
I deployed my program in release instead of debug, and the EXE file now works on the other computer.
I haven't seen that specific error before. Usually it's an error around a missing DLL (Windows redistributable). Assuming there isn't actually a problem with the configuration, you have two choices:
Change the compile mode from Multithreaded DLL to Multithreaded. This can be done from the C++ section of project properties under code generation. In multithreaded mode your binary will be statically linked against the Windows redistributable. This is probably what you want.
Install the Windows redistributable on the target machine. This probably isn't OK, because you state that you don't want to install anything on the target machine.
A warning about option 1: Different versions of Windows have different versions of the redistributable. It's possible to encounter a highly specialized environment in which a statically linked program will not behave as expected.
It look like you're missing some DLL files. Be sure to copy appropriate DLL files along with EXE file.
I am running Visual Studio 2019 and I found a very helpful configuration property to address the problem of moving a simple application to another computer without an installation package.
Open the project Property Pages.
Choose which configurations this change should apply to, I used “All Configurations”.
In the left-hand window click to expand the top node called “Configuration Properties”.
Click on "Advanced". In the right-hand window look for the property called “Copy C++ Runtime to OutDir” and set that to “yes”.
Click OK to close the Properties window.
Rebuild your project. All the necessary dlls will be copied to the project’s output directory. Copy your exe and all dlls to another computer. The exe should find everything it needs to run.

How to convert MVS C++ code into executable application?

So I've made a small program in Microsoft Visual Studio C++ (2008 edition) and I want to convert it to .exe format.
I know that I can find the .exe in my project directory, however it only runs fine on my own PC. When I send it over to someone else (who doesn't have Visual Studio installed or anything), it doesn't run.
I've read a little about this and it seems to be about "linking". Now I don't know anything about linking and I'm not eager to learn in depth about it right now...
I just want to make my incredibly simple program (which edits and creates text files) to run in .exe program on any computer. Is this possible, and if so - how?
Could you please guide me through the steps? I'm quite new to programming so I really need help on this one.
Thank you in advance...
The file you sent already is an exe file, but it is most likely a debug version of the program. Also, you need to see if you are linking against other DLLs, and if you are, you should distribute those with the program as well.
Since you have a simple program, it is most likely you have no other DLL dependencies, so try compiling in release mode as a first step. There will be a dropdown at the top of VS with Debug and Release, and Debug is currently selected.
You already have the .exe file that is created by linking all the .obj files that are compiled from the .cpp files you created. To repeat myself, the linking is already happening behind the scenes. However, there are two other issues. First, you may have compiled it in debug mode. In order to run your program on another Windows machine, it is best to compile it in release mode. Also, if your program uses DLLs, such as for MFC classes, then you need to copy those DLLs to the other computer as well. In order to do this correctly, you should look at creating a setup.exe file which will correctly install your program and all the required DLLs. There are tools that can help you do this, such as InstallShield.
I think you should install runtime components of Visual C++ Libraries : "Microsoft Visual C++ 2008 Redistributable Package (x86)" required to run applications developed with Visual C++ on a computer that does not have Visual C++ 2008 installed.
http://www.microsoft.com/en-us/download/details.aspx?id=29

[VC++]How can I run my program on another PC?

I have an MFC application that runs on my computer, but when I try to run it on another PC I receive an error message that the application failed to initialize and I should re-install it. What should I do?
See redistributing an MFC application from MSDN.
Generally you could install the Redistributing Visual C++ package for x86.
You can also compile your program to statically link the MFC dlls for easier deployment.
The problem is that the other computer is missing the C/C++ runtime libraries. Install the version of the Microsoft Visual C++ runtime that matches the version of Visual C++ that you used to compile it on the other computer.
Alternatively, you can link the application statically. Right click on the project in the Solution Explorer and choose Properties, then naviagate to C/C++ > Code Generation > Runtime Libraries. Choose the Debug/Release option that doesn't include "DLL".
You can link static or install the VC++ Redistributable Pack.
You are missing some of the libraries, either link the application statically or install the VS redistributables on the other computer
See http://www.microsoft.com/downloads/details.aspx?FamilyID=9b2da534-3e03-4391-8a4d-074b9f2bc1bf
If you get a message like "side-by-side configuration error", that means your exe must be shipped some extra files, probably MFC / ATL dlls. Use dependency walker to find out what are the dependencies : http://www.dependencywalker.com/