如何在 Visual Studio 2015 中使用 clang 为 Windows 编译 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31351372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compile C++ for Windows with clang in Visual Studio 2015
提问by MikeMB
As far as I understand, Visual Studio 2015 is shipped with clang. First I though this was only for Android and iOS apps, but according to this articleit should also be possible to use the clang++ frontend for Windows programs. However, I can't find the according option.
据我了解,Visual Studio 2015 附带了 clang。首先,我虽然这仅适用于 Android 和 iOS 应用程序,但根据本文,它也应该可以为 Windows 程序使用 clang++ 前端。但是,我找不到相应的选项。
So could you please explain to me, how I can change the used compiler to clang in a c++ project (in VS2015 RC Community Edition).
所以请您向我解释一下,我如何在 c++ 项目(在 VS2015 RC 社区版)中将使用的编译器更改为 clang。
采纳答案by Oktalist
Starting with VS2015 Update 1 you can install the "Clang with Microsoft CodeGen" template via the New Project window, browse to Installed -> Templates -> Visual C++ -> Cross Platform.
从 VS2015 Update 1 开始,您可以通过 New Project 窗口安装“Clang with Microsoft CodeGen”模板,浏览到 Installed -> Templates -> Visual C++ -> Cross Platform。
You will then be able to choose the Clang 3.7 with Microsoft CodeGen v140_clang_3_7
Platform Toolset in the properties page of any Windows C++ project.
然后,您将能够v140_clang_3_7
在任何 Windows C++ 项目的属性页面中选择带有 Microsoft CodeGen平台工具集的 Clang 3.7 。
Clang has a completely different set of command-line options which it recognizes, so when using the v140_clang_3_7
toolset, the project properties must be using Clang-compatible options, which can be recognized as starting with -
instead of /
. For example, Debug Information Format property set to "Full Debug Information (DWARF2) (-g2 -gdwarf-2
)", and Enable C++ Exceptions property set to "Yes (-fexceptions
)".
Clang 有一组完全不同的命令行选项可以识别,因此在使用v140_clang_3_7
工具集时,项目属性必须使用与 Clang 兼容的选项,可以识别为以开头-
而不是/
. 例如,Debug Information Format 属性设置为“Full Debug Information (DWARF2) ( -g2 -gdwarf-2
)”,Enable C++ Exceptions 属性设置为“Yes ( -fexceptions
)”。
There's more information in this blog post.
这篇博文中有更多信息。
回答by Trinidad
As far as I understand it both Clang and GCC are shipped with the Android and iOS crossplatform SDKs/tools for Visual Studio 2015.
据我了解,Clang 和 GCC 都随附于 Visual Studio 2015 的 Android 和 iOS 跨平台 SDK/工具。
From what I've seen it only allows me to choose those while having one of those crossplatform projects.
从我所看到的,它只允许我在拥有这些跨平台项目之一的同时选择那些。
Using the template project for a GLES C++ application you get the following options:
使用 GLES C++ 应用程序的模板项目,您将获得以下选项:
While for a Windows C++ application you get the dialog below where you can see that Windows target platform is grayed out and read-only, meaning you probably have a set of toolkits for each target platform, but you simply cannot change it, at least for now.
对于 Windows C++ 应用程序,您会看到下面的对话框,您可以在其中看到 Windows 目标平台呈灰色且只读,这意味着您可能为每个目标平台都有一组工具包,但您根本无法更改它,至少对于现在。
Have no idea how you turn a normal VS project into crossplatform though, and it's likely that you can only target Android or iOS out of the box using 3rd party compilers.
不知道如何将普通 VS 项目转换为跨平台,并且很可能您只能使用 3rd 方编译器开箱即用地针对 Android 或 iOS。
It may be possible, though, to install Clang as another toolkit in the same way the XP toolkits are. So perhaps it's just a matter of someone fiddling with it and making it available as it is already installed.
但是,可以像 XP 工具包一样将 Clang 安装为另一个工具包。因此,也许这只是某人摆弄它并在它已经安装时使其可用的问题。
Caveat: This answer is pre-VS2015 update 1 which didn't have the "Clang with Microsoft CodeGen" option.
警告:此答案是 VS2015 更新 1 之前的版本,它没有“Clang with Microsoft CodeGen”选项。
回答by MikeMB
After reading through the linked post and especially the comments again, I came to understand, that this is not a feature shipped with VS2015 RC but a possible future feature that might e.g. be shipped in a SP or (more likely) with the next version of VS.
在阅读了链接的帖子,尤其是再次阅读评论后,我开始明白,这不是 VS2015 RC 附带的功能,而是未来可能的功能,例如可能会在 SP 中提供或(更有可能)与下一版本的对比。
As mentioned by sjdowling, the closest thing you can do at the moment (October 2015, clang 3.7) is to download and install llvm for windows. This should give you a LLVM-vs2014 platform toolset option. For me it works for simple test programs, but apparently this version of "clang-vs" seems to not yet support exceptions. However, according to these notes, that problem should be mostly solved for clang 3.8.
正如 sjdowling 所提到的,目前您可以做的最接近的事情(2015 年 10 月,clang 3.7)是下载并安装llvm for windows。这应该会给你一个 LLVM-vs2014 平台工具集选项。对我来说,它适用于简单的测试程序,但显然这个版本的“clang-vs”似乎还不支持异常。但是,根据这些说明,该问题应该在 clang 3.8 中主要解决。
回答by marsh
While the above solutions work they require that you have installed clang which is off by default in the visual studio 2015 installation. The accepted answer does show how to install it, though the next page will ask you to close visual studio which you are running the new project from.
虽然上述解决方案有效,但它们要求您安装了在 Visual Studio 2015 安装中默认关闭的 clang。接受的答案确实显示了如何安装它,尽管下一页会要求您关闭正在运行新项目的 Visual Studio。
If anyone is wondering how to install clang because it does not show up in their list here is another solution:
如果有人想知道如何安装 clang 因为它没有出现在他们的列表中,这里是另一种解决方案:
- Control Panel->Programs->Programs and Features.
- Right click on “Microsoft Visual Studio Enterprise 2015” (or “Microsoft Visual
- Studio 15 Preview” if you have the preview of the next version of Visual Studio installed)
- Click “Change”
- Click “Modify”
- Select “Clang with Microsoft CodeGen” – The March 2016 release.
- 控制面板->程序->程序和功能。
- 右键单击“Microsoft Visual Studio Enterprise 2015”(或“Microsoft Visual
- Studio 15 预览版”(如果您安装了下一版 Visual Studio 的预览版)
- 点击“更改”
- 点击“修改”
- 选择“Clang with Microsoft CodeGen”——2016 年 3 月发布。
https://blogs.msdn.microsoft.com/vcblog/2016/03/31/clang-with-microsoft-codegen-march-2016-released/
https://blogs.msdn.microsoft.com/vcblog/2016/03/31/clang-with-microsoft-codegen-march-2016-released/