Package C console application (Visual Studio '13) - c

I wrote a basic program for my mom, and now I want her to be able to use it. Obviously, it works on my computer. Getting the .exe file from the project folder, and putting it on her computer doesn't work: it says MSVCR120d.dll is missing whenever the .exe is run. Makes sense--as her computer doesn't have Visual Studio on it. However, I tried installing the Visual C++ Redistributable Packages for Visual Studio 2013, and that didn't work either.
To be honest, I'm not looking to spending hours of time to piece this all together. This is something I will more than likely never do again--I've already done some searching and can only find subjects speaking of C++ distributions. I want a way to get the console app on her computer to work.

The more easy way is link statically. That mean embed all the needed code to the app to run, in the final binary (.exe), eliminating dependency of other libraries.
Go to Project Properties
Go to Configuration Properties
Go to C/C++
Go to Code Generation
Change Runtime Library (in Debug to Multi-Threaded Debug /MTd and in Release to Multi-Threaded /MT)

Related

Can Visual Studio 2019 pack the DLLs it requires in just a small .exe file?

I made a Windows application with C++ WinAPI by using Visual Studio 2019.
After finishing, I built it, and executed with my computer. It works perfectly.
Then I sent to my friend who didn't have Visual Studio. It said that it needs "msvcp140d.dll" "ucrtbased.dll" "vcruntime140_1d.dll" "vcruntime140d.dll" to open it.
Then I found them in my computer, put them in the same dir with my application, and sent them to my friend. It worked fine, too.
But My question is "Is there any way to pack them with just Visual Studio 2019?" A small application with lots of DLLs is just a little bit weird.
First you're sending the wrong files. Files with d suffix like that are for debugging only and must not be distributed
You cannot redistribute all of the files that are included in Visual Studio; you are only permitted to redistribute the files that are specified in Redist.txt or the online "REDIST list." Debug versions of applications and the various Visual C++ debug DLLs are not redistributable. For more information, see Choosing a Deployment Method.
Determining Which DLLs to Redistribute
Final executable files must be compiled in release mode and use the release version of those DLLs. Don't give out debug binaries. They're seriously slow due to the logics added for debugging purposes
And you don't actually need to send the DLLs but you should tell the user to install the corresponding VC redistributable package. It's the runtime (CRT) for Visual Studio projects containing functions like printf, memcpy... for you. You don't need to find any other DLL if you don't use any DLLs in the project
It's also possible to link the runtime library statically by changing the option /MD to /MT. That way the final exe file will be self-contained (no need for additional runtime DLLs) but it'll also be larger and you lose the ability to use the newer library functions when the package is updated to fix bugs or performance issues. Again, you must compile in release mode regardless of whether you're linking statically or dynamically
See also
Compile to a stand-alone executable (.exe) in Visual Studio
Compile C in Visual Studio 2012 without MSVCRT runtime
How to make a Single Executable VS 2010

Visual Studio Code does not include C header files

I recently downloaded Visual Studio Code to begin learning the C programming language. I installed the program as well as the C extension. However, when I tried to create the "Hello, World!" program, it would not run, and in the Problems menu it did not recognize the stdio.h header file, saying that I need to update my includePath. I have not been able to find any stdio.h file on my computer to link to. Do I need to download the C library files (even though I have read they should be included with the compiler), and if so, where can I find them? Or is there another solution? Thanks, and sorry if this is a stupid question, I am new to this.
I think you might be confusing VS Code with the VS IDE.
VS Code is a source editor only; that is to say that it's basically just a glorified text editor. It has the ability to load extensions and open a shell to compile the code, and there are a few extensions that let you debug the code itself, but they can be tricky to get setup and installed to work well with C/C++ code. VS Code does not have a compiler/assembler/linker nor the requisite headers or SDK's as that is up to you (the user) to install and then point to those in your settings file.
The Visual Studio IDE, on the other hand, is a complete integrated development environment that also includes the system headers and SDK's for Windows, as well as the binaries to properly compile, link and assemble your code into a binary for a Windows system (cross platform is possible as well). The Visual Studio IDE comes in many different flavors with the latest being VS 2017.
If you wish to stick with VS Code, you'll need to grab a compiler and the appropriate header files for the system you're targeting. If you wish to just stick with Windows for now, you can grab the Windows 7 SDK here or the Windows 10 SDK here .. you could even grab both and just reference the one you wish when you want. Note that the Windows 7 SDK includes the Microsoft C/C++ compiler, alternatively you can download the MSVC compiler from their Build Tools site.
There's also Cygwin in which you can use the GNU compiler, and of course Clang, which can be referenced in both VS Code and the VS IDE.
I hope that can help.

A visual studio solution that does one nmake

I'm not really a visual studio developer here. Recently, I was pulled out for a much bigger team to quickly configure and compile a C library for a visual studio project. It took a while going through the README for the library, but I figured out that it basically came down to:
1) Open the VC Command Prompt
2) Run nmake on a .mak file
3) That was it, your dll gets spit out.
So I give it over to the guy, and he says "This is fine and all, but what I really want is a visual studio solution and project." I'm not really a visual studio developer though. All I want is one solution where the compile process is "run nmake". But going through documentation seems to imply that nmake is just a command-line tool. I'm sort of a VS noob though. Is this something you can even do?
To build a VS solution/project, you can use msbuild. See the MSDN documentation.
Example:
MSBuild.exe MyProject.proj /t:rebuild

MSVCR120.DLL Required

I was asked to download the specified DLL after running one of my apps on a secondary computer. The only problem is whether or not it should have been required. I obviously code in Visual Studio, but it is in pure "C" and a Win32 project, so I'm just wondering if this is normal.
if you don't want to load CRTs, change run time library to static library. (MT)
or you need these dlls in installed visual studio\VC\redist
//after your comments//
MSxx##D.dll D means Debug.
if you link any library built with debug run-time, the exe needs debug runtime library

How do I compile C++/CLI code for Silverlight?

I have a C++/CLI library that I would like to use in a Silverlight application. It is supposed to be possible to write code for Silverlight in any .NET language, but so far I've only worked out how to compile C#. Silverlight does not seem to be able to use DLLs compiled for .NET.
I'm using Visual Studio 2010 and Silverlight 4. The only new projects available for Silverlight are C# projects. Porting the code to C# is not a practical option.
How do I compile C++/CLI code for Silverlight?
I think I may have gotten a VS2010 C++/CLI class library project to build with references to (only) Silverlight assemblies.
Update
Ok, it is possible. But it is not nice.
First, you must convince the C++ compiler to NOT load the .NET Framework, using an undocumented compiler switch. But that's not the worst part.
Set your C++/CLI project "Common Language Runtime Support" to /clr:safe
Next, under References, remove all references.
Next, in the C++/CLI project properties, under C++ > Command Line, enter /d1clr:nomscorlib /FU"C:\Program Files (x86)\Microsoft Silverlight\4.0.50917.0\mscorlib.dll"
Now, save the project and exit Visual Studio. Open the .vcxproj in a text editor, and change the framework version setting. You want it to be the same as a C# Silverlight project:
<TargetFrameworkIdentifier>Silverlight</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<SilverlightVersion>$(TargetFrameworkVersion)</SilverlightVersion>
Now, reopen Visual Studio, and build the project. You'll get an error because the compiler auto-generated a file with #using<mscorlib> and the search path finds the .NET Framework version first.
Silverlight,Version=v4.0.AssemblyAttributes.cpp(1): fatal error C1197: cannot reference 'c:\windows\microsoft.net\framework\v4.0.30319\mscorlib.dll' as the program has already referenced 'c:\program files (x86)\microsoft silverlight\4.0.50917.0\mscorlib.dll'
Double-click the error to open the auto-generated file. Replace the path-less reference with e.g. (here's where you put your references, not in the project properties)
#using <c:\program files (x86)\microsoft silverlight\4.0.50917.0\mscorlib.dll>
#using <c:\program files (x86)\microsoft silverlight\4.0.50917.0\System.dll>
#using <c:\program files (x86)\microsoft silverlight\4.0.50917.0\System.Core.dll>
Luckily, the compiler leaves your changes in-place. So you should be good as long as no one cleans your temp directory.
Building should now succeed.
Then, you need to go add the DLL created by the C++/CLI project to your Silverlight application. Note that you can't set up a project reference, because VS2010 still isn't convinced that the C++/CLI is a Silverlight project. So you'll have to browse and add the reference as an assembly file. (And it won't automatically switch between Debug and Release to match the Silverlight application).
Final Notes
I got it to run an empty Silverlight application in Debug mode and stop at a breakpoint in the middle of C++/CLI code. Also the C++/CLI code successfully returned a value to C# and the local variable in C# received the correct value. So I guess it's working.
I went through a bunch more steps trying to make this work, but I don't think they affected the outcome. If you run into errors, though, let me know and I'll try to figure out what I omitted from this answer.
Ben Voigt, thanks for this, it worked for me too.
Also, if your C++ code does anything that is specific to the C++ language (i.e. not entirely IL portable) like using stack semantics for an array, you'll get the following error:
could not find assembly 'Microsoft.VisualC.dll' etc.
If you recompile with the full .NET Framework and then dump the IL code, you'll find references to "''.$ArrayType$$$BY06$$CB_W modopt" or something similar. This tells you where to change the code.
I found that after I installed the Silverlight SDK and it got added to "\Program Files(x86)\Reference Assemblies" I did not have to go through all of Ben Voigt's steps, just changing the project file was enough.
Another note, you can also use:
<TargetFrameworkProfile>WindowsPhone71</TargetFrameworkProfile>
if you want to target Windows Phone (install the SDK first).
Silverlight does not support native C++ libraries, nor any P/Invoke scenarios due to security concerns. If your library is pure .Net you might be able to decompile it with ILDASM and recompile for Silverlight with ILASM.
Silverlight is not a powerful development platform like .NET which is tightly integrated with operating system. First of all silverlight is supposed to run on any operating system, so there is no choice of Native API anywhere in silverlight.
Silverlight also does not support MSIL completely, so there is lot of problem in compiling and recomiping it at IL level.
Can you say more about what kind of C++/CLI code you have? Most Rich internet applications (Silverlight's target) do not include any of high powerful computation, instead they are plain simple HTML+JS alternatives. For powerful graphics, you can use Silverlight's PixelShadder support.
Reflector
What you can do alternatively is,
Compile your C++/CLI to regular .NET DLL, use Reflector to disassemble and generate C# source code from your dll, it may not be perfect, but you will have most of your logic converted back in C#.
I was able Ben Voigt's solution to work with some minor changes in Visual Studio 2013
Here is what I did different.
Unload the silverlight project that you want to reference it in. Right click and choose edit project.csproj
Copy the Target Framework Settings. For me this was
<TargetFrameworkIdentifier>Silverlight</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v5.0</TargetFrameworkVersion>
<SilverlightVersion>$(TargetFrameworkVersion)</SilverlightVersion>
For the compiler switch /d1clr:nomscorlib This was not working for me. Also a reference to mscorlib for silverlight was automatically added to the complied output without specifying it on the command line options. This is how I got around it.
Open the project in Just Deompile
Load the Assembly Editor Plugin
Navigate the tree to the references
Delete the reference to the non silverlight mscorlib from the reflexil menu.
Right click on the top level of the tree for the assembly, and save as under the reflexil menu.
I haven't tested all of the functions, but the ones I have tested so far worked as expected.
Thank you Ben, your post has saved me a lot of time. I was thinking I was going to have to port my library:)

Resources