1

I'm building a Windows executable with VS 2019. When I run it on my machine, it works, but I'm not 100% sure it will work for end users who don't have vc_redist.x64.exe version 2019. (Especially users on Win7 - it's in a niche where users still use this version).

How to statically link everything so that the end user will never be asked to download and install Visual C++ Redistributable "vc_redist"?

I'm using msbuild.exe, and no IDE. Which setting to add in the .vcxproj file or in the .cpp file to enable full static linking, to prevent the need for vcredist?

My .cpp code asks for these libraries:

#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "winmm.lib")

Sample .vcxproj:

<Project DefaultTargets="Build" ToolsVersion="16.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|x64">
      <Configuration>Release</Configuration>
      <Platform>x64</Platform>
    </ProjectConfiguration>    
  </ItemGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.default.props" />
  <PropertyGroup>
    <ConfigurationType>Application</ConfigurationType>
    <PlatformToolset>v142</PlatformToolset>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  <ItemGroup>
    <ClCompile Include="main.cpp" />
  </ItemGroup>
  <ItemGroup>
    <ClInclude Include="main.h" />
  </ItemGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Targets" />
</Project>

Note: it is linked with the topic How to deploy a Win32 API application as an executable, but here it's about doing it specifically directly from the .vcxproj file and without the IDE.

Basj
  • 41,386
  • 99
  • 383
  • 673
  • you need static link crt libs ( https://learn.microsoft.com/en-us/cpp/c-runtime-library/crt-library-features?view=msvc-170 ) – RbMm Mar 16 '22 at 09:37
  • Thanks @RbMm. There seems to be many lib combinations and many `/MT`, `/MDd`, ... parameters. By the way these are parameters for `cl.exe`, is that right? I don't use `cl.exe` directly, but `msbuild` with a `.vcxproj` file. Where to set all static linking options in a `.vcxproj` file or the `.cpp` file? – Basj Mar 16 '22 at 09:41
  • the *cl.exe* used in any case. i be first set this options in vs studio ui and look for generated vcxproj file – RbMm Mar 16 '22 at 09:46
  • @RbMm I don't use VS IDE / UI, it's not installed, I only have the build tools. Would you have any idea? – Basj Mar 16 '22 at 09:48
  • 1
    i not use direct vcxproj files (only vs studio). look https://learn.microsoft.com/en-us/cpp/build/reference/project-files?view=msvc-170 in section you need parameter. in current example MultiThreadedDLL but you need instead MultiThreadedDLL - static link (not dll) but i not sure which exactly word need use – RbMm Mar 16 '22 at 10:02

1 Answers1

2

Add a specific ClCompile property for the compilation configuration:

<Project DefaultTargets="Build" ToolsVersion="16.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    ...
    <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
        <ClCompile>
          <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
        </ClCompile>
    </ItemDefinitionGroup>
</Project>

Possible values are here: runtimeLibraryOption Enum (remove the "rt" prefix)

rtMultiThreaded 0 : Multi-threaded (/MT)

rtMultiThreadedDebug 1 : Multi-threaded Debug (/MTd)

rtMultiThreadedDebugDLL 3 : Multi-threaded Debug DLL (/MDd)

rtMultiThreadedDLL 2 : Multi-threaded DLL (/MD)

More info about MT / MD can be found here: /MD, /MT, /LD (Use Run-Time Library)

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • Thanks! How did you know it's `rtMultiThreaded` and not `rtMultiThreadedDLL`? Is there a doc stating that `rtMultiThreaded` means no use of vc_redist libraries? – Basj Mar 16 '22 at 10:36
  • 2/2: if I want to apply this setting to all platforms / all targets, can I do just `MultiThreaded` without any `Condition`? – Basj Mar 16 '22 at 10:37
  • 1
    @Basj - I've not tested, but yes, it should work. The doc is in this link https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.vcprojectengine.runtimelibraryoption from there you can see MultiThreaded corresponds to /MT documented here: https://learn.microsoft.com/en-us/cpp/build/reference/md-mt-ld-use-run-time-library – Simon Mourier Mar 16 '22 at 10:58