将 LLVM Clang 4.x.x / 5.x.x / 6.x.x 集成到 Visual Studio 2017

Integrate LLVM Clang 4.x.x / 5.x.x / 6.x.x into Visual Studio 2017

official LLVM 4.0 build for Windows 与 Visual Studio 集成至 Visual Studio 2015。不幸的是,它仍然不支持 Visual Studio 2017。

当您尝试将项目的 Platform Toolset 设置为 LLVM-vs2014 时,它会弹出一个错误。

你知道有什么方法可以让它发挥作用吗?


更新

2018年,LLVM 6.0官方仍然不支持与Visual Studio 2017(版本15.X.X)集成,仅支持与Visual Studio 2015(版本14.X.X)集成工具集。

它需要一些仅随 C++ v140 工具集一起提供的 msbuild 目标,而 VS 2017 默认仅安装 v141 工具集。如果您打开 VS 2017 安装程序,找到 v140 工具集的复选框并安装它,那么正确的 C++ msbuild 目标将可用,并且一切正常。

LLVM/Clang 现在有一个更新的补丁,允许您将它与 VS2017 一起使用。但是他们仍然不直接支持VS2017。我在 LLVM 开发人员邮件列表上请求他们更新对 VS2017 的支持,所以希望他们会这样做。如果他们听我的话。

我已经弄清楚如何将 LLVM Clang 7.0 与 Visual Studio 2017 更新 15.5.6 集成。 v1913 使用最新的 LLVM 版本完全支持基于 PDB 的调试。

即lld-link/DEBUG:GHASH; clang-cl -mllvm -emit-codeview-ghash-section 标志到 clang-cl。

这是一个三步过程。

  1. 安装最新的 llvm
  2. 更新 VS 中的 toolset.props、toolset.targets 以支持最新的 clang
  3. Select 用于构建 C/C++ 或其他 lang 项目的新工具集

自 Visual Studio 2017 更新 15.4.5 起,Microsoft "experimental" Clang C2 不再有效。因此,上述修复对于使用 clang 编译具有完整 PDB(不仅仅是 CodeView /Z7 限制)可调试性的代码是必要的。这现在也成为一个更完整的可移植性测试跨平台构建套件,因为您可以使用从 clang 编译器前端到 LLVM 代码生成后端和 LLVM link 的所有 LLVM 组件为 windows 构建和 PDB 调试呃

干杯,大卫

2018 年 1 月 9 日结帐http://planet.clang.org/

查看"Try it out!"部分:

If you're already using clang-cl and lld-link on Windows today, you can try this out. There are two flags needed to enable this, one for the compiler and one for the linker: To enable the emission of a .debug$H section by the compiler, you will need to pass the undocumented -mllvm -emit-codeview-ghash-section flag to clang-cl (this flag should go away in the future, once this is considered stable and good enough to be turned on by default). To tell lld-link to use this information, you will need to pass the /DEBUG:GHASH to lld.

您只需要在您的 c++ 项目 "Command Line:Additional Options" 区域中传递 -mllvm -emit-codeview-ghash-section 标志,或者直接将它们放在 "toolset.props"您在 C:\Program Files (x86)\Microsoft Visual Studio17\Enterprise\Common7\IDE\VC\VCTargets\Platforms\Win32\PlatformToolsets\LLVM-vs2017 中创建的文件或 C:\Program Files (x86)\Microsoft Visual Studio17\Enterprise\Common7\IDE\VC\VCTargets\Platforms\x64\PlatformToolsets\LLVM-vs2017.

关键是在添加那些 cli 选项时,您告诉 clang 发出 的调试信息lld(又名 lld-link)将理解并用于生成完全填充的 PDB 文件。不是它在 2018 年 1 月 9 日发布 LLVM 7.0 之前制作的有限

toolset.targets:(任何版本)

<Project ToolsVersion="14.1" 
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(VCTargetsPath)\Microsoft.CppCommon.targets" />
</Project>

toolset.props: (Win32版本)

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">  
  <Import Project="$(VCTargetsPath)\Platforms$(Platform)\PlatformToolsets\v141\Microsoft.Cpp.$(Platform).v141.props" Condition="Exists('$(VCTargetsPath)\Platforms$(Platform)\PlatformToolsets\v141\Microsoft.Cpp.$(Platform).v141.props')"/>
  <Import Project="$(VCTargetsPath)\Platforms$(Platform)\PlatformToolsets\v141\Toolset.props" Condition="Exists('$(VCTargetsPath)\Platforms$(Platform)\PlatformToolsets\v141\Toolset.props')"/>

  <PropertyGroup>
    <LLVMInstallDir>$(Registry:HKEY_LOCAL_MACHINE\SOFTWARE\LLVM\LLVM)</LLVMInstallDir>
    <LLVMInstallDir Condition="'$(LLVMInstallDir)' == ''">$(Registry:HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\LLVM\LLVM)</LLVMInstallDir>
    <ExecutablePath>$(LLVMInstallDir)\msbuild-bin;$(ExecutablePath)</ExecutablePath>
    <LibraryPath>$(LLVMInstallDir)\lib\clang.0\lib\windows;$(LibraryPath)</LibraryPath>
  </PropertyGroup>

  <ItemDefinitionGroup>
    <ClCompile>
      <!-- remove the implicit vcxxx.pdb path to avoid rebuilds every time as clang-cl only supports /Z7 -->
      <ProgramDataBaseFileName></ProgramDataBaseFileName>
      <!-- Set the value of _MSC_VER to claim for compatibility -->
      <AdditionalOptions>-m32 -fmsc-version=1913 %(AdditionalOptions)</AdditionalOptions>
    </ClCompile>
  </ItemDefinitionGroup>
</Project>

对于 x64,将 -m32 更改为 -m64

p.p.s.,我还启用了 Microsoft 的 ARM 和 ARM64 编译器来构建原生 Windows-10-ARM 应用程序(不是 UWP modern-com-junk)。但是,到目前为止,我还没有对 clang 资源进行足够的挖掘,无法为 ARM 正确配置类似于 -m32-m64Intel code-gen.

查看这些文章:

最后,我找到了 brilliant GitHub repo 和所需的 MSBuild 平台工具集,将 LLVM clang 5.0.0 集成到 Visual Studio 2017 。按照 README 文件的说明进行操作后,您将拥有两个新的平台工具集 LLVM-vs2017LLVM-vs2017_xp。问题已解决。

更新

我制作了一个 fork,它针对 LLVM 6.0.0 进行了更新,并通过提供 LLVM/clang 的包含和库路径来提供更好的集成。

感谢 Royi,他意识到原来的 .prop 文件是为 LLVM 5.0 明确定制的,它没有添加正确的 lib ( $(LLVMInstallDir)\lib) 和 include ($(LLVMInstallDir)\lib\clang.0.0\include) 个文件夹。

LLVM 项目现在通过 https://marketplace.visualstudio.com/items?itemName=LLVMExtensions.llvm-toolchain

明确支持 Visual Studio 2017

我是 LLVM 技术的新手,我正在使用 Visual Studio 扩展 Clang Power Tools。他们有一个设置页面,您可以从中安装 LLVM(所有版本 >= 4.0)。之后,您可以使用 VS 工具栏中的扩展按钮自由应用 clang compile 或 tidy with code modernization(这是我最欣赏的)。这样你不需要配置任何东西。

更新

从顶部打开扩展设置和select LLVM 页面。在 LLVM 页面上,您将看到所有受支持的 LLVM 版本,并且在每个版本的右侧都有安装按钮。安装您需要的任何版本。页面底部有一个下拉菜单,允许您 select 如果您安装了多个版本,要使用哪个版本。

在此 blog post

中逐步解释了该功能