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

4

54

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..
Crissie answered 27/8, 2015 at 1:11 Comment(1)
See also github.com/boostorg/hana/wiki/Setting-up-Clang-on-WindowsKalina
J
11

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

Jessy answered 27/8, 2015 at 5:27 Comment(2)
Thanks! I downloaded clang 3.7.0 from the pre-release page here: llvm.org/pre-releases/3.7.0 and installed the 4.9.3 version of mingw-w64: sourceforge.net/projects/mingw-w64/files/…, made sure both had their bin directory on the path and it worked! i.imgur.com/E1XKaeY.pngCrissie
I ended up installing both the 64 and the 32 bit versions of clang 3.7.0 and mingw-w64 (version 5.1.0, posix threads with seh exceptions for 64 and dwarf exceptions for 32 bit). I put them in their default locations and only put the 64 bit version on the path. Then I made two batch files to temporarily add the 32 bit versions to the path when needed: gist.github.com/JohannesMP/1e7ed200367460255971 - for example when using drmemory which only supports 32 bit executables. This works great and I can compile and link with clang/gcc in both 32 and 64 bit.Crissie
S
18

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.

Sihonn answered 27/8, 2015 at 1:21 Comment(14)
Interesting. I will give that a shot. Is MSYS2 a package manager like brew/apt-get? I haven't heard of it before.Crissie
MSYS2 is an environment for software development on Windows, and it does have a package manager (pacman). It's great!Sihonn
Ok, I've tried installing it and running the first command you mentioned. I'm getting a bunch of 404's: i.imgur.com/DVKtj74.png any ideas?Crissie
Your package database is probably out of date; MSYS2 uses gcc 5.2.0 nowadays. Try carefully following the "Updating packages" instructions from sourceforge.net/p/msys2/wiki/MSYS2%20installationSihonn
I've updated my packages, successfully run pacman -S mingw-w64-x86_64-clang and was able to build the exe as I wanted. It runs fine from inside the mingw64 shell, but when I attempt to open the exe from windows explorer I get several missing dll's. I recall from when I set up mingw32 on my linux server to compile exe's that one way to fix this was to statically link the libraries for the corresponding dll's. Is there a way to automate that without adding -static-libstdc++, etc? Again, going for simplicity here. I realize that in larger projects you can just include it in MAKE files.Crissie
After some searching this question here: https://mcmap.net/q/270727/-how-to-do-static-linking-of-libwinpthread-1-dll-in-mingw answered it. it's possible to provide the -static flag and the compiler will try to include libs when possible. I understand that this comes at a cost of increased executable size and possible memory usage but that's acceptable.Crissie
Copying the required runtime DLLs to the same directory as your executable is another way to let it run.Sihonn
Yep! it should also be noted (if only for reference of anyone reading this in the future) that the dynamic linking of libraries is not limited to MSYS2's executables, but also affects executables built with clang 3.7.0 as mentioned above (which I'm most likely going to stick with since it can also work with 32 bit). In any case, thanks so much for your help David. I'm choosing the other answer due to its simplicity, but I will definitely also be using msys2 in the future.Crissie
MSYS2 has a 32-bit clang too. Just install mingw-w64-i686-clang and run the Win32 shellSihonn
Let us continue this discussion in chat.Crissie
What's the difference between pacman -S mingw-w64-x86_64-clang and pacman -S clang? I see two different names and versions, but I'm not entirely sure where it gets clang fromKymberlykymograph
The first clang would itself be a native Windows program and live in /mingw64. The latter clang would probably be an msys2 program (using the MSYS2 POSIX emulation stuff and depending on msys-2.0.dll) and live in /usr/bin. Each version of clang likely is configured to produce executables for the same runtime that clang itself uses, but I'm not too sure, and I know one build of clang is able to target multiple platforms. All official MSYS2 package recipes are in these two repos: github.com/AlexPux/MINGW-packages github.com/AlexPux/MSYS2-packagesSihonn
Why couldn't they just call it clang?Umbra
If you are referring to the MSYS2 package name, all MSYS2 packages that run with MinGW runtime are prefixed with mingw-w64. That's just how they keep their packages organized.Sihonn
J
11

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

Jessy answered 27/8, 2015 at 5:27 Comment(2)
Thanks! I downloaded clang 3.7.0 from the pre-release page here: llvm.org/pre-releases/3.7.0 and installed the 4.9.3 version of mingw-w64: sourceforge.net/projects/mingw-w64/files/…, made sure both had their bin directory on the path and it worked! i.imgur.com/E1XKaeY.pngCrissie
I ended up installing both the 64 and the 32 bit versions of clang 3.7.0 and mingw-w64 (version 5.1.0, posix threads with seh exceptions for 64 and dwarf exceptions for 32 bit). I put them in their default locations and only put the 64 bit version on the path. Then I made two batch files to temporarily add the 32 bit versions to the path when needed: gist.github.com/JohannesMP/1e7ed200367460255971 - for example when using drmemory which only supports 32 bit executables. This works great and I can compile and link with clang/gcc in both 32 and 64 bit.Crissie
S
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
Saddlebow answered 29/3, 2018 at 10:16 Comment(4)
What are the system variables needed for CMake to recognize Clang besides having CLang on the system path?Keeshakeeshond
I have no experience with CMake. So I am afraid you have to ask a new question or search an existing one.Saddlebow
CMake just looks for the default system variable installation of CLang adds to Windows system. What are they?Keeshakeeshond
I just manually add the LLVM executable path to my system PATH variable, something like d:\Program Files\LLVM\bin.Saddlebow
H
0

Depending on what users need, the existing answers are either out-of-date, incomplete, or not comprehensive enough, and the blog post linked in the question is a dead link.

The initial question was:

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.

There exists an official way to do this. Admittedly not as simple as sudo apt install -y clang && clang hello.cpp, but still relatively straightforward compared to Cygwin/MSYS2/MinGW:

  1. Download and install the Microsoft Build Tools for Visual Studio 2022.
  2. In the installer that appears, select the 'Desktop Development with C++' workload, and under 'Installation details' to the right, select 'C++ Clang tools for Windows':VS Installer: Clang config
  3. Proceed with the install; a restart may be necessary.
  4. From the Start menu, open the 'Developer PowerShell for VS 2022'. This sets up the environment variables for you, and the shortcut may be named differently.
  5. Navigate to wherever your C/C++ source is. In PowerShell, cd works as expected; for a more idiomatic command, use sl (alias for Set-Location).
  6. Run clang.exe to compile your file with all options as you would, just like on Linux. Note that this install of clang will use the MSVC C and C++ static/dynamic libraries (ucrt.dll).
    • Bonus: clang-cl.exe is a clang driver that is compatible with most options from Microsoft's own compiler, cl.exe (which is included in the above setup). From this presentation, clang-cl.execlang.exe --driver-mode=cl. So, cl.exe users can directly migrate to clang with minimal fuss, and something like clang-cl.exe /std:c++20 /EHsc /O2 /Fe:main.exe main.cpp works perfectly.
Heliotrope answered 31/8, 2023 at 21:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.