C++ 让 Clang 在 Windows 上工作

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

Getting Clang to work on windows

c++mingweclipse-cdtcodeblocksclang

提问by Luther

I have followed the following step by step guideand I've managed, after a bit of fiddling, to get clang to compile using code:blocks and MinGW. Great, so now I could add the Clang module to eclipse (why have one IDE when you can have four) and start compiling.

我已经按照以下分步指南进行了操作,经过一番折腾,我设法使用 code:blocks 和 MinGW 来编译 clang。太好了,现在我可以将 Clang 模块添加到 eclipse(为什么有一个 IDE,而你可以有四个)并开始编译。

I can compile a simple program that doesn't use the standard library but unfortunately when I try to compile this:

我可以编译一个不使用标准库的简单程序,但不幸的是,当我尝试编译它时:

#include <iostream>
using namespace std;

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

first of all I get this:

首先我得到这个:

..\src\test.cpp:9:10: fatal error: 'iostream' file not found

..\src\test.cpp:9:10: 致命错误:找不到“iostream”文件

so I add the Mingw headers to the include path; then I get this:

所以我将 Mingw 头文件添加到包含路径中;然后我得到了这个:

'fatal error: 'bits/c++config.h' file not found'

'致命错误:'bits/c++config.h' 文件未找到'

which is weird. Why does MingW work if that file isn't in 'bits/'? Is it built in to the compiler?. Never mind, I find an implementation of it and create the file in 'bits/'.

这很奇怪。如果该文件不在“位/”中,为什么 MingW 可以工作?它是内置在编译器中的吗?没关系,我找到了它的实现并在“位/”中创建了文件。

Then I get a whole storm of errors including strange ones that seem to suggest either clang doesn't implement the preprocessor correctly or else my understanding of the preprocessor is incorrect.

然后我得到了一大堆错误,包括奇怪的错误,这些错误似乎表明 clang 没有正确实现预处理器,或者我对预处理器的理解是不正确的。

C:\Program Files\CodeBlocks\MinGW\lib\gcc\mingw32.4.1\include\c++/cwchar:45:26: error: expected value in expression
#if _GLIBCXX_HAVE_WCHAR_H

and many more like that. Should that be

还有更多类似的。应该是

#if defined(_GLIBCXX_HAVE_WCHAR_H) 

or

或者

#ifdef _GLIBCXX_HAVE_WCHAR_H?

If they are then the MinGW standard libraries are wrong.

如果是,则 MinGW 标准库是错误的。

I assume I'm incorrect in assuming that clang can be dropped in to replace gcc and that it is not designed to work with the gnu standard libraries. Any confirmation or denial of this, backed up with evidence would be most welcome!

我假设我假设可以使用 clang 来替换 gcc 并且它不是为与 gnu 标准库一起使用而设计的,我认为这是不正确的。任何对此的确认或否认,并有证据支持将是最受欢迎的!

So, does anybody have a foolproof way to get clang compiling on a Windows PC? There's a dearth of information online regarding clang and especially regarding windows.

那么,有没有人有一种万无一失的方法来在 Windows PC 上进行 clang 编译?网上缺乏关于 clang 的信息,尤其是关于 windows 的信息。

I'm really keen to get clang and LLVM working as they sound great from what I've read. Any help would be appreciated.

我真的很想让 clang 和 LLVM 工作,因为从我读过的内容来看,它们听起来很棒。任何帮助,将不胜感激。

Thanks.

谢谢。

采纳答案by Mike Dinsdale

There's some instructions for building clangon this page(hidden in the "Clang Development" part of the sidebar...). For MinGW you want the section called "On Unix-like Systems". The only tricky part is step 5 which tells you how to set up the paths for the C++ standard library. These need to be added into the code in clang/lib/Frontend/InitHeaderSearch.cpp. On my machine it wound up looking like this

clang此页面上有一些构建说明(隐藏在侧边栏的“Clang Development”部分...)。对于 MinGW,您需要名为“在类 Unix 系统上”的部分。唯一棘手的部分是第 5 步,它告诉您如何设置 C++ 标准库的路径。这些需要添加到clang/lib/Frontend/InitHeaderSearch.cpp. 在我的机器上它看起来像这样

// FIXME: temporary hack: hard-coded paths.
AddPath("/usr/local/include", System, true, false, false);
AddPath("c:/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/include/c++", System, true, false, false);
AddPath("c:/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/include/c++/mingw32", System, true, false, false);
AddPath("c:/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/include/c++/backward", System, true, false, false);
AddPath("c:/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../../include", System, true, false, false);
AddPath("c:/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/include", System, true, false, false);
AddPath("c:/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/include-fixed", System, true, false, false);

though I'm not sure all these are needed!

虽然我不确定所有这些都需要!

回答by Matthieu M.

Depending on your version of MinGW(and thus the version of gcc ported), the headers might be scattered a bit...

根据您的版本MinGW(以及移植的 gcc 版本),标题可能会有点分散......

In the file clang/lib/Frontend/InitHeaderSearch.cppyou will find a number of hard-coded paths. The trouble is that each is version specific, so if your version of MinGW is not in there, then feel free to add it (and ask for it to be integrated in Clang's mainline by posting the patch to cfe-commit).

在该文件中,clang/lib/Frontend/InitHeaderSearch.cpp您将找到许多硬编码路径。问题是每个版本都是特定的,所以如果你的 MinGW 版本不在那里,那么请随意添加它(并通过将补丁发布到 cfe-commit 来要求将它集成到 Clang 的主线中)。

Personally I run it on MinGW/msys with only minor issues (a number of test cases fail because my msys shell mangles the input when there are :in...), I have not tried using it from CodeBlocks though (I'm used to the command line).

就我个人而言,我在 MinGW/msys 上运行它,只有一些小问题(许多测试用例失败,因为我的 msys shell 在输入时破坏了输入:......),但我还没有尝试从 CodeBlocks 使用它(我已经习惯了命令行)。

If you wish to help, Takumi is watching over MinGW integration, Francois Pichet is leading the work on compatibility with VC++/MFC headers (ie is the main contributor) and @rubenvbis currently trying to push patches on libc++ to have it working on Windows (libc++ does not compile on Windows yet). The 3 areas are pretty much independent and require different skills and knowledge.

如果你想提供帮助,Takumi 正在关注 MinGW 的集成,Francois Pichet 正在领导与 VC++/MFC 头文件的兼容性工作(即是主要贡献者),@rubenvb目前正在尝试在 libc++ 上推送补丁以使其在 Windows 上运行(libc++ 还不能在 Windows 上编译)。这三个领域几乎是独立的,需要不同的技能和知识。

回答by user

I had a similar problem. I used the GCC 4.7's analogs of the paths specified by Mike Dinsdale's answer and specified them with the '-isystem' flag (Clang 3.2 in the mingw64 distribution as built by rubenvb) to all of my future calls to the clang executable (via scripts). As these directories are being specified explicitly as systeminclusion directories, all potentially wearisome warnings generated by them are automatically suppressed.

我有一个类似的问题。我使用了由 Mike Dinsdale 的答案指定的路径的 GCC 4.7 类似物,并使用“-isystem”标志(由 rubenvb 构建的 mingw64 发行版中的 Clang 3.2)指定它们到我未来对 clang 可执行文件的所有调用(通过脚本)。由于这些目录被明确指定为系统包含目录,因此它们生成的所有潜在的令人厌烦的警告都会被自动抑制。

tl;dr: the -isystemflag specifies systeminclusion directories without recompilationin Clang

tl; dr:-isystem标志指定系统包含目录,无需在 Clang 中重新编译

回答by jalf

Clang does have hardcoded search locations, as defined in the file clang/lib/Frontend/InitHeaderSearch.cpp, near the comment FIXME: temporary hack: hard-coded paths.

Clang 确实有硬编码的搜索位置,如文件中所定义clang/lib/Frontend/InitHeaderSearch.cpp,靠近注释FIXME: temporary hack: hard-coded paths

There's a note about it on this page: http://clang.llvm.org/get_started.html

这个页面上有一个关于它的说明:http: //clang.llvm.org/get_started.html

So get the include paths from your other compiler (MingW), and hardcode them into Clang, and it mightwork. (I'm not sure if Clang's Windows support is 100% there yet)

所以从你的其他编译器 (MingW) 获取包含路径,并将它们硬编码到 Clang 中,它可能会起作用。(我不确定 Clang 的 Windows 支持是否达到 100%)

回答by Engineer

Windows 10 / VS 2017 / Clang 4.0.0missing stdlib.hin C code. This is how I solved it:

stdlib.hC 代码中缺少Windows 10 / VS 2017 / Clang 4.0.0。我是这样解决的:

  • Open x86_x64 Cross Tools Command Prompt for VS 2015/2016/2017. Running clang in here should eliminate the "missing headers" error.
  • If still not working, you are missing the actual headers and/or libs, since Clang leaves these platform-specifics to VS or MinGW. Go to \Program Files (x86)\Windows Kits\10\Include\and search its subdirectories (if any) for stdlib.hand co. If not found, you will need to install the latest Windows 10 SDKby going to Visual Studio (installer) and clicking Modifyto add packages.
  • 打开x86_x64 Cross Tools Command Prompt for VS 2015/2016/2017。在这里运行 clang 应该消除“缺少标题”错误。
  • 如果仍然无法正常工作,则您将缺少实际的头文件和/或库,因为 Clang 将这些平台特定信息留给了 VS 或 MinGW。转到\Program Files (x86)\Windows Kits\10\Include\并搜索其子目录(如果有)stdlib.h和 co。如果未找到,则需要Windows 10 SDK通过转到 Visual Studio(安装程序)并单击修改以添加包来安装最新版本。

回答by Chawathe Vipul S

When I had the same issue, I just set-up Code Blocks to the correct include directories for my setup. And it uses -I with clang++ and everything's been working great so far.

当我遇到同样的问题时,我只是将代码块设置为我的设置的正确包含目录。它使用 -I 和 clang++,到目前为止一切都很好。