您需要安装什么才能在 Windows 上使用 Clang 为 64 位构建 C++14?

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

What do you need to install to use Clang on windows to build c++14 for 64 bit?

windowsc++11gccclang++llvm-clang

提问by Johannes

UPDATE:

更新:

I've written a detailed tutorial that incorporates the top two answers on this question: http://blog.johannesmp.com/2015/09/01/installing-clang-on-windows-pt1/

我写了一个详细的教程,其中包含了这个问题的前两个答案:http: //blog.johannesmp.com/2015/09/01/installing-clang-on-windows-pt1/





TL;DR

TL; 博士

On Windows, Given the following program:

在 Windows 上,给定以下程序:

#include <iostream>

int main()
{
    int arr[] = {1, 2, 3, 4, 5};
    for(auto el : arr)
    {
        std::cout << el << std::endl;
    }
    return 0;
}

I want to be able to do the following:

我希望能够做到以下几点:

clang++ hello.cpp -o hello.exe -std=c++14

And get a 64 bit executable that just works. I don't want to have to append a ton of -Iincludes to tell clang where to find iostreamor other standard c++ headers; I don't want to have to link in multiple steps.

并获得一个可以正常工作的 64 位可执行文件。我不想附加大量的-I包含来告诉 clang 在哪里可以找到iostream或其他标准的 C++ 头文件;我不想在多个步骤中链接。

I don't care so much about performance, efficiency, what linker is used, etc. I just want to be able to have clang/gcc/whatever set up correctly so that I can use a single, quick and dirty, console command like the one above.

我不太关心性能、效率、使用什么链接器等。我只是希望能够正确设置 clang/gcc/whatever 以便我可以使用单个、快速和肮脏的控制台命令,例如上面那个。

What do I need to install for that to just work?

我需要安装什么才能正常工作



The Problem

问题

As a predominately mac/linux user I'm used to being able to just use a package manager to install the latest version of clang, which just works.

作为主要使用 mac/linux 的用户,我习惯于仅使用包管理器来安装最新版本的 clang,它可以正常工作

I'm now trying to set up clang/gnu compiler on windows and it seems to be far more difficult, If only because there is little to no straightforward documentation out there (that I've been able to find)

我现在正在尝试在 Windows 上设置 clang/gnu 编译器,这似乎要困难得多,如果只是因为那里几乎没有直接的文档(我已经能够找到)

I've tried to follow this tutorial: https://yongweiwu.wordpress.com/2014/12/24/installing-clang-3-5-for-windows- and was able to use it to get clang to build and link (using gcc 4.8.2), but the resulting binaries were 32 bit.

我尝试按照本教程进行操作:https: //yongweiwu.wordpress.com/2014/12/24/installing-clang-3-5-for-windows- 并且能够使用它来构建和链接 clang (使用 gcc 4.8.2),但生成的二进制文件是 32 位的。

I've tried installing the latest prebuilt binaries of clang (3.6.2) and the 64 bit version of mingw-w64 (4.9.3 for 64 bit with posix and sjlj for exceptions), and am getting:

我已经尝试安装最新的 clang ( 3.6.2) 和 64 位版本的 mingw-w64 ( 4.9.3 for 64 bit with posix and sjlj for exceptions)的最新预构建二进制文件,并且得到:

hello.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
         ^
1 error generated.

Which seems to indicate that clang is not seeing gcc's files. It seems that some versions of LLVM/clang are looking for only certain versions of gcc, but that doesn't seem to be documented anywhere?

这似乎表明 clang 没有看到 gcc 的文件。似乎某些版本的 LLVM/clang 只寻找特定版本的 gcc,但似乎没有任何地方记录?

Similarly someone mentioned to me on the LLVM IRC that you need to modify clang's driver to look for gcc in certain paths?

同样,有人在 LLVM IRC 上向我提到您需要修改 clang 的驱动程序以在某些路径中查找 gcc?



What I'm looking for

我在找什么

I'm fine with building LLVM/Clang from source if necessary, but I'm really just looking for clear, step-by-step instructions that allow me to use clang++as easily as I'm able to do with mac/linux

如有必要,我可以从源代码构建 LLVM/Clang,但我真的只是在寻找清晰的分步说明,让我可以clang++像使用 mac/linux 一样轻松使用

Something like:

就像是:

  1. Build this versionof LLVM/Clang and place it in that directory
  2. Download and install this versionof cygwin (or mingw32 or mingw-w64) and install these packages.
  3. etc..
  1. 构建this versionLLVM/Clang 并将其放入that directory
  2. 下载并安装this versioncygwin(或 mingw32 或 mingw-w64)并安装these packages.
  3. 等等..

采纳答案by user5271266

if you use the upcoming clang 3.7.0, simply set PATH to include mingw-w64's bin, clang will work as you expect

如果您使用即将推出的 clang 3.7.0,只需将 PATH 设置为包含 mingw-w64 的 bin,clang 将按您的预期工作

回答by David Grayson

Try installing MSYS2and then installing the mingw-w64-x86_64-clang (binary) package:

尝试安装MSYS2,然后安装 mingw-w64-x86_64-clang(二进制)包:

pacman -S mingw-w64-x86_64-clang

It is a bit old at the moment (3.6.2), but it might be good enough for you. Then when you run the Win64 shell provided by MSYS2, clangwill be on your path.

目前它有点旧(3.6.2),但它可能对你来说已经足够了。然后当您运行 MSYS2 提供的 Win64 shell 时,clang将在您的路径上。

If it's not good enough, I have recently been building a 64-bit version of clang with MSYS2 and using it to compile native 64-bit Windows executables. My process was something like:

如果还不够好,我最近一直在用 MSYS2 构建 64 位版本的 clang,并使用它来编译本机 64 位 Windows 可执行文件。我的过程是这样的:

  1. Use pacman to install base-devel, mingw-w64-x86_64-ninja, mingw-x86_64-cmake and perhaps some other packages that are needed by the build process.
  2. Grab my PKGBUILD script for clang and the files in the same directory. This is based on the mingw-w64-clang-svnpackage from MSYS2, which is largely the work of Martell Malone. You can find him on the MSYS2 IRC channel and ask him more about it.
  3. In a Win64, shell, go to the directory with my PKGDUILD, run export MINGW_INSTALLS=mingw64(so you are only compiling the 64-bit version), and then run makepkg-mingw.
  1. 使用 pacman 安装 base-devel、mingw-w64-x86_64-ninja、mingw-x86_64-cmake 以及构建过程中可能需要的其他一些包。
  2. 获取我的 PKGBUILD 脚本以获取 clang 和同一目录中的文件。这是基于MSYS2的mingw-w64-clang-svn包,这主要是 Martell Malone 的工作。您可以在 MSYS2 IRC 频道上找到他,并向他询问更多相关信息。
  3. 在 Win64、shell 中,转到包含我的 PKGDUILD 的目录,运行export MINGW_INSTALLS=mingw64(因此您只编译 64 位版本),然后运行makepkg-mingw.

It is unlikely you will get everything right on the first try, and some files might need to be edited. Clang may have changed since the last time I did this and some patches might not apply cleanly.

您不太可能在第一次尝试时就做对了,有些文件可能需要编辑。自从我上次这样做以来,Clang 可能已经发生了变化,有些补丁可能无法完全应用。

回答by jdhao

You can install llvm pre-release binary for Windows here. MinGW-w64 can be downloaded here. Of course, you should make sure the paths are properly set up.

您可以在此处为 Windows 安装 llvm 预发布二进制文件。MinGW-w64 可以在这里下载。当然,您应该确保路径设置正确。

For the latest version of clang, e.g., clang 6.0.0. The above solution by @user5271266 will not be enough. Now the default target for clang Windows is x86_64-pc-windows-msvc(Assume that you are using 64 bit Windows).

对于最新版本的 clang,例如clang 6.0.0. @user5271266 的上述解决方案是不够的。现在,clang Windows 的默认目标是x86_64-pc-windows-msvc(假设您使用的是 64 位 Windows)。

In order to compile C++ source files, according to here, we should change the target:

为了编译C++源文件,根据这里,我们应该改变目标:

clang++ -target x86_64-pc-windows-gnu -std=c++14 test.cc -o test.exe