C++ Windows 上的 Clang

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8882753/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:16:31  来源:igfitidea点击:

Clang on Windows

c++windowsmingwllvmclang

提问by Alexander Shukaev

First of all, I've followed "Getting Started: Building and Running Clang". In particular, I've built it according to "Using Visual Studio" section. In other words, I've built it using Visual Studio 2010.

首先,我关注了“入门:构建和运行 Clang”。特别是,我根据“使用 Visual Studio”部分构建了它。换句话说,我使用 Visual Studio 2010 构建了它。

Secondly, I've manually set include and library paths to MinGW distribution:

其次,我手动设置了 MinGW 分发的包含和库路径:

enter image description here

在此处输入图片说明

The simple program I'm trying to compile:

我正在尝试编译的简单程序:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

I get the following feedback from the compiler:

我从编译器那里得到以下反馈:

In file included from C:\MinGW\lib\gcc\mingw32.5.2\include\c++\iostream:39:
In file included from C:\MinGW\lib\gcc\mingw32.5.2\include\c++\ostream:39:
In file included from C:\MinGW\lib\gcc\mingw32.5.2\include\c++\ios:38:
In file included from C:\MinGW\lib\gcc\mingw32.5.2\include\c++\iosfwd:41:
In file included from C:\MinGW\lib\gcc\mingw32.5.2\include\c++\bits/postypes.h:41:
C:\MinGW\lib\gcc\mingw32.5.2\include\c++\cwchar:144:11: error: no member named 'fgetws' in the global namespace
  using ::fgetws;
        ~~^
C:\MinGW\lib\gcc\mingw32.5.2\include\c++\cwchar:146:11: error: no member named 'fputws' in the global namespace
  using ::fputws;
        ~~^
C:\MinGW\lib\gcc\mingw32.5.2\include\c++\cwchar:150:11: error: no member named 'getwc' in the global namespace
  using ::getwc;
        ~~^
C:\MinGW\lib\gcc\mingw32.5.2\include\c++\cwchar:151:11: error: no member named 'getwchar' in the global namespace
  using ::getwchar;
        ~~^
C:\MinGW\lib\gcc\mingw32.5.2\include\c++\cwchar:156:11: error: no member named 'putwc' in the global namespace
  using ::putwc;
        ~~^
C:\MinGW\lib\gcc\mingw32.5.2\include\c++\cwchar:157:11: error: no member named 'putwchar' in the global namespace
  using ::putwchar;
        ~~^
6 errors generated.
Build error occurred, build is stopped
Time consumed: 646  ms.  

The obvious question is - why do I get this?

显而易见的问题是 - 为什么我会得到这个?

Additionally, I would like to know more details, and since, Clang website provides extremely brief information - I thought that somebody could clarify the following questions to me:

此外,我想了解更多细节,因为 Clang 网站提供了非常简短的信息 - 我认为有人可以向我澄清以下问题:

  1. As far as I understand Clang does not have its own standard library (stdc++ I guess, isn't it?). That's why I have to use MinGW's headers and libraries - am I right?
  2. What is the difference between building Clang with Visual Studio and MinGW?
  3. Do I have to hard-code include paths in clang/lib/Frontend/InitHeaderSearch.cppor I can skip it and rather specify those paths later through "-I" option as I do in the screenshot above?
  1. 据我了解,Clang 没有自己的标准库(我猜是 stdc++,不是吗?)。这就是为什么我必须使用 MinGW 的头文件和库 - 对吗?
  2. 使用 Visual Studio 和 MinGW 构建 Clang 有什么区别?
  3. 我是否必须硬编码包含路径,clang/lib/Frontend/InitHeaderSearch.cpp或者我可以跳过它,而是稍后通过“-I”选项指定这些路径,就像我在上面的屏幕截图中所做的那样?

回答by rubenvb

If you build Clang with MSVS, it will automatically search the default VS include paths, and pull in those headers. This is the reason the libstdc++ headers are producing errors: they are importing C functions not present in the VS headers. Using Clang for C++ with VS is for now a no-go: you will get link failures due to missing ABI (name mangling and others) functionality in Clang. If you still want to use the MSVS Clang, don't point it to MinGW headers. It will parse the VS headers (including C++), it just will fail to link.

如果您使用 MSVS 构建 Clang,它会自动搜索默认的 VS 包含路径,并拉入这些标头。这就是 libstdc++ 头文件产生错误的原因:它们导入了 VS 头文件中不存在的 C 函数。将 Clang for C++ 与 VS 一起使用现在是行不通的:由于缺少 Clang 中的 ABI(名称修改和其他)功能,您将遇到链接失败。如果您仍想使用 MSVS Clang,请不要将其指向 MinGW 标头。它将解析 VS 标头(包括 C++),只是无法链接。



EDIT: I have built a dw2 version of GCC (32-bit only) accompanied by Clang. Exceptions work in this build, and so you can build real C++ stuff with Clang now on Windows. Get version 3.2 here.

编辑:我已经构建了一个带有 Clang 的 dw2 版本的 GCC(仅限 32 位)。异常在此构建中有效,因此您现在可以在 Windows 上使用 Clang 构建真正的 C++ 内容。在此处获取 3.2 版

回答by Chawathe Vipul S

The obvious answer is you forgot sending -fno-ms-compatibility to clang++ :P

显而易见的答案是您忘记将 -fno-ms-compatibility 发送到 clang++ :P

  1. You are right.
  2. VC++ is GUI tool-chain, MinGW is character console.
  3. No as clang is mature enough for public consumption but still to stabilize so let its dev team work on the code otherwise you risk work that might become lost island. I'm using -I as your example suggests.
  1. 你是对的。
  2. VC++是GUI工具链,MinGW是字符控制台。
  3. 不,因为 clang 已经足够成熟以供公众使用,但仍处于稳定状态,因此请让其开发团队在代码上工作,否则您可能会冒着工作可能成为迷失岛的风险。我正在使用 -I 作为您的示例所建议的。

I'm doing windows app dev using VS and use clang+CodeBlocks for sharing aspects neutral to platform's domain.

我正在使用 VS 进行 Windows 应用程序开发,并使用 clang+CodeBlocks 来共享与平台域无关的方面。