静态链接所有依赖项,以便最终用户永远不会被要求安装 vc_redist.exe

发布于 2025-01-14 16:02:42 字数 1855 浏览 3 评论 0原文

我正在使用 VS 2019 构建一个 Windows 可执行文件。当我在我的计算机上运行它时,它可以工作,但我不能 100% 确定它是否适用于没有 vc_redist.x64.exe< 的最终用户/code> 版本 2019。(特别是 Win7 上的用户 - 它处于用户仍然使用此版本的利基市场)。

如何静态链接所有内容,以便最终用户永远不会被要求下载和安装 Visual C++ Redistributable "vc_redist"?

我正在使用 msbuild.exe,并且没有 IDE。 要在 .vcxproj 文件或 .cpp 文件中添加哪个设置来启用完全静态链接,以防止需要 vcredist?

我的.cpp 代码需要这些库:

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

示例 .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>

注意:它与主题 如何将 Win32 API 应用程序部署为可执行文件 ,但这里是直接从 .vcxproj 文件执行此操作,无需 IDE。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一绘本一梦想 2025-01-21 16:02:42

为编译配置添加特定的 ClCompile 属性:

<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>

可能的值如下: runtimeLibraryOption 枚举
(删除“rt”前缀)

rtMultiThreaded 0:多线程 (/MT)

rtMultiThreadedDebug 1:多线程调试(/MTd)

rtMultiThreadedDebugDLL 3:多线程调试 DLL (/MDd)

rtMultiThreadedDLL 2:多线程 DLL (/MD)

有关 MT / MD 的更多信息可以在这里找到:/MD、/MT、 /LD(使用运行时库)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文