What do you need to install to use Clang on window

2019-03-08 05:15发布

问题:

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/



TL;DR

On Windows, Given the following program:

#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 -I includes to tell clang where to find iostream or other standard c++ headers; I don't want to have to link in multiple steps.

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.

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.

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)

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.

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:

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?

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


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

Something like:

  1. Build this version of LLVM/Clang and place it in that directory
  2. Download and install this version of cygwin (or mingw32 or mingw-w64) and install these packages.
  3. etc..

回答1:

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



回答2:

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

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, clang will be on your path.

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:

  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-svn package 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.

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.



回答3:

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.

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).

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

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