Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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..
like image 785
Johannes Avatar asked Aug 27 '15 01:08

Johannes


People also ask

Can you use Clang on Windows?

Getting Clang on WindowsOn Windows, it's easy to install the Clang tools. Just grab the “Clang compiler for Windows,” an optional component of the “Desktop development with C++” workload. This will install everything you need to develop with Clang on Windows.

Does Clang work for C++?

If you are looking for source analysis or source-to-source transformation tools, Clang is probably a great solution for you. Clang supports C++11, C++14 and C++17, please see the C++ status page for more information.


1 Answers

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.

like image 100
David Grayson Avatar answered Oct 05 '22 20:10

David Grayson