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

11,612

Solution 1

Finally, I found a brilliant GitHub repo with the required MSBuild platform toolsets which integrates LLVM clang 5.0.0 into Visual Studio 2017. After following the instructions of the README file, you will have two new platform toolsets LLVM-vs2017 and LLVM-vs2017_xp. Problem solved.

Update

I made a fork which is updated for LLVM 6.0.0 and provides better integration by providing include and library paths of LLVM/clang.

Thanks to Royi, who realized that the original .prop files are explicitly tailored for LLVM 5.0 and it misses adding the proper lib ( $(LLVMInstallDir)\lib) and include ($(LLVMInstallDir)\lib\clang\6.0.0\include) folders.

Solution 2

It requires some msbuild targets that only ship with the C++ v140 toolset, and VS 2017 only installs the v141 toolset by default. If you open the VS 2017 installer, find the checkbox for the v140 toolset and install that then the right C++ msbuild targets will be available and the thing will work.

Solution 3

The LLVM project now explicitly supports Visual Studio 2017 via https://marketplace.visualstudio.com/items?itemName=LLVMExtensions.llvm-toolchain

Solution 4

I'm a newbie with the LLVM technology and I'm using a Visual Studio extension, Clang Power Tools. They have a settings page from where you can install LLVM (all versions >= 4.0). After that you are free to apply clang compile or tidy with code modernization(this is what I appreciate the most) by using the extension buttons from VS toolbar. In this way you don't need to configure anything.

Update

Open the extension settings and select the LLVM page from the top. On the LLVM page, you'll see all the supported LLVM versions and at the right of each version the install button. Install any version you need. On the bottom of the page is a dropdown that allows you to select what version to use in case you installed multiple versions.

The feature is explained step by step in this blog post

Solution 5

I have figured out how to integrate LLVM Clang 7.0 with Visual Studio 2017 update 15.5.6. v1913 with full support for PDB based debugging using the latest LLVM builds.

i.e., lld-link /DEBUG:GHASH; clang-cl -mllvm -emit-codeview-ghash-section flag to clang-cl.

It is a three step process.

  1. Install latest llvm
  2. Update the toolset.props, toolset.targets in VS to support latest clang
  3. Select the new toolset to use for building your C/C++ or other lang project

As of Visual Studio 2017 update 15.4.5 the Microsoft "experimental" Clang C2 no longer works. Thus, the above fixes are necessary to use clang to compile code that has full PDB (not just CodeView /Z7 limited) debuggability. This also now becomes a more complete suite for portability testing cross-platform builds since you can build and PDB debug for windows using all LLVM components from the clang compiler front end to the LLVM codegen backend and LLVM linker.

Cheers, David

Share:
11,612

Related videos on Youtube

plasmacel
Author by

plasmacel

I wrote this bio strictly for the Autobiographer badge.

Updated on June 04, 2022

Comments

  • plasmacel
    plasmacel almost 2 years

    The official LLVM 4.0 build for Windows integrates with Visual Studio up to Visual Studio 2015. Unfortunately it still doesn't support Visual Studio 2017.

    When you try to set the Platform Toolset of a project to LLVM-vs2014, it pops up an error.

    Do you know any way to make it work?


    Update

    In 2018, LLVM 6.0 officially still doesn't support integration with Visual Studio 2017 (version 15.X.X), only with the Visual Studio 2015 (version 14.X.X) toolset.

  • keith
    keith almost 7 years
    Yes it would be good of them to support v141 without the need for v140 :-)
  • keith
    keith almost 7 years
    Would also be good of them to change the name from LLVM-vs2014!
  • Sam Hobbs
    Sam Hobbs over 6 years
    This sounds subject to change. Is there something official we can look at that will give us current information? I can't find the checkbox for the v140 toolset; I would look closer but if it now does not exist then I won't.
  • keith
    keith over 6 years
    @user34660, there's a tab called "Individual Components", it's still there on that tab!
  • plasmacel
    plasmacel about 6 years
    @keith The problem is finally solved. See my own answer.
  • plasmacel
    plasmacel about 6 years
    @batbrat See my answer for the required MSBuild toolsets.
  • Royi
    Royi almost 6 years
    Will it create files which are compatible with Windows yet are optimized by Clang? For instance, will I have updated OpenMP support?
  • plasmacel
    plasmacel almost 6 years
    @Royi Yes. Not only optimized by clang, but completely compiled by clang. However if you want OpenMP support, you will need libomp and enable it by a compiler switch. See openmp.llvm.org.
  • plasmacel
    plasmacel almost 6 years
    @Royi In newer LLVM versions it is enabled by default, yeah. However, unfortunately, the latest LLVM binary distribution doesn't include libomp. So maybe you have to build it on your own.
  • plasmacel
    plasmacel almost 6 years
    @Royi If you mean the "Pre-Built Binaries" distribution, then no. Unfortunately, lately they removed libomp from the binary distribution. However if you download the sources, you can build libomp yourself.
  • plasmacel
    plasmacel almost 6 years
    @Royi As I remember the last LLVM version where they included libomp in the binary distribution was LLVM 5.0. You can also download previous versions on the linked download page.
  • plasmacel
    plasmacel almost 6 years
    @Royi You can also try to copy libomp.lib and libomp.dll from a binary distribution which includes them to the binary distribution of the newest version. You have to simply put them into the bin folder.
  • plasmacel
    plasmacel almost 6 years
  • plasmacel
    plasmacel almost 6 years
    @Royi I successfully built the OpenMP libraries for LLVM 6.0 using Visual Studio 2017 for 32bit and 64bit targets.
  • plasmacel
    plasmacel over 5 years
    @Royi Yeah, I built the whole LLVM 6.0.1 for Windows, which includes all of its components (note that the official distribution on LLVM's website lacks about 75% of LLVM).
  • plasmacel
    plasmacel over 5 years
  • Royi
    Royi over 5 years
    Is there a way to get only OpenMP? When you say you build, is this the build for Windows (Namely cl like?) Or the original Clang? I meant adding only OpenMP to the repository.
  • plasmacel
    plasmacel over 5 years
    @Royi LLVM is inherently a cross-compiler. clang is just a compiler frontend for it. clang-cl is just a shortcut for clang --driver-mode=cl, so you can use clang as cl on any supported platform, not just Windows. So if you need, it is "cl like" on any platform, you can build for Windows on any platform. I shared the Windows binary distribution with you. If you only need libomp, then locate its related files.
  • plasmacel
    plasmacel over 5 years
    @Royi Actually it is a Windows x64 build, so you cannot use it with 32-bit targets. I don't support 32-bit targets anymore.
  • sandthorn
    sandthorn over 5 years
    It seems that -mllvm -emit-codeview-ghash-section become unrecognized option in clang 8.0 as of SVN r347735 (29 November 2018) llvm.org/builds . While llds option /DEBUG:GHASH still produces the .pdb file.
  • Royi
    Royi about 5 years
    plasmacel, Any chance you have Clang with OpenMP support in version 7.1 and 8.0?
  • zhisme
    zhisme over 3 years
    however links may answer the question, they can become invalid in future, consider adding some quotations to your answer and then refer to blog posts.