30

At the Going Native conference last week, Chandler Carruth announced the existence of prebuilt binaries for running clang on windows. The same information is in a blog post here. The intended audience for this is users of Visual Studio, but I want to run clang from the command line.

I ran the installer and added the LLVM bin directory to my path, but when I try to compile "Hello world", I get this:

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

I can't find any information on how to configure things to run clang on Windows, and I'm guessing that after I figure out how to tell clang where to search for standard library headers, I'll have to tell it where to look for libraries to link with. Can somebody walk me through the setup step by step or point me to such a walkthrough?

KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91
  • Shouldn't it be `clang++`? Not sure it matters, though. – Vittorio Romeo Sep 10 '13 at 06:15
  • @VittorioRomeo: Same result with clang++ (which is exactly the same size as clang.exe). – KnowItAllWannabe Sep 10 '13 at 07:10
  • Alright. Second idea: search the path where `iostream` is located, and add it to `clang++` flags with `-I` – Vittorio Romeo Sep 10 '13 at 07:12
  • It's only an alpha release, and they're "able to build a trivial C++ application". The intended audience is actually "developers interested in helping us track down and understand bugs". Unless you are such a developer, you should wait at least until there's a beta release. – molbdnilo Sep 10 '13 at 07:14
  • 1
    I guess you should first run `VS2012 x86 Native Tools Command Prompt` or things like this, add `clang++.exe` to the path, and then try it. – Siyuan Ren Sep 10 '13 at 08:30
  • Did you ever solve this? I am still getting errors like this with LLVM 3.4 for Windows, for instance with stdio.h in a c (not c++) file – Baruch Jan 13 '14 at 21:13
  • I gave up. I want to run Clang on Windows, but I don't have time for a research project. I'll wait for others to do the heavy lifting for me. – KnowItAllWannabe Jan 14 '14 at 00:52
  • Clang on Windows is a work in progress and Chandler Carruth said in his Going Native 2013 talk that iostreams was one of the things that do not currently work. – pierre best Sep 11 '13 at 14:07

4 Answers4

21

This is a old question, and a lot has changed since then. Given this is a common problem when trying Clang on Windows, it deserves an updated answer.

As of 2017, with the LLVM 3.9.1 build for Windows, you need the following to be able to invoke clang from your shell.

VC++ Build Tools

We still do not have a libc++ port for Windows, so Clang uses the VC++ libraries as well as the VC++ linker.

So first of all you need the VC++ Build Tools on your system. Do note you already have those installed if you happen to have the Visual C++ IDE.

Environment Variables

You need to tell Clang where to find the build tools and its libraries.

Option 1 (vcvarsall.bat)

This is the easiest and standard option.

Run

> "%VS140COMNTOOLS%../../VC/vcvarsall.bat" amd64

Replacing amd64 with your target architecture on Clang, which may be x86, amd64 or arm. You may replace %VS140COMNTOOLS% as well if you have a different version of the VC++ toolset.

As a shortcut, you could run the Visual C++ Command Prompt instead of cmd+vcvarsall, since you need to call this batch for every command prompt you open.

Now you are able to enjoy Clang.

Option 2 (Manually)

In case you cannot run vcvarsall.bat or want to automate this process, welcome, I had the same need.

All of the following environments variables are set automatically by vcvarsall.bat, so you can run that and take your machine values from there. I'll give mines as examples and in the hope it's the same elsewhere.

Set the INCLUDE environment variable to C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE;C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt;C:\Program Files (x86)\Windows Kits\8.1\include\shared;C:\Program Files (x86)\Windows Kits\8.1\include\um;C:\Program Files (x86)\Windows Kits\8.1\include\winrt;

Set LIB to C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\LIB\amd64;C:\Program Files (x86)\Windows Kits\10\lib\10.0.10240.0\ucrt\x64;C:\Program Files (x86)\Windows Kits\8.1\lib\winv6.3\um\x64;. Do note the architecture specific components!

For the build tools, you can either have the tools on PATH or setup the VCINSTALLDIR environment variable. Clang will try both, favoring VCINSTALLDIR.

Set VCINSTALLDIR to %VS140COMNTOOLS%../../VC or add %VS140COMNTOOLS%../../VC/bin/amd64 to your PATH.

Footnote

This is all very under documented, so the requirements may change at any time, but the Clang MSVC driver is trying to automate this as much as possible, by querying the Windows Register and many other tricks, so none of this may be necessary anymore in the future.

Community
  • 1
  • 1
Denilson Amorim
  • 9,642
  • 6
  • 27
  • 31
  • Are you sure Clang actually reads those environment variables? Or are you assuming some build system that does. – Timmmm Apr 24 '17 at 10:05
  • Also if I do it via command line flags I still get errors - `expected ';' after to level declarator expanded from '_CRT_BEGIN_C_HEADER'` in `vcruntime.h` and many more. – Timmmm Apr 24 '17 at 10:08
  • 1
    @Timmmm Yes, quite sure, you can read some of the MSVC driver [here](https://github.com/llvm-mirror/clang/blob/master/lib/Driver/ToolChains/MSVC.cpp) and search for those environment vars. Currently using `clang++` directly, so yeah, no build system. I got the Win64 installer from [releases.llvm.org](http://releases.llvm.org/download.html), tested with Clang 3.9.1 and 4.0.0. – Denilson Amorim Apr 24 '17 at 16:30
7

If you are not restricted to use Microsoft compilers. You can use clang with MinGW-w64. Just install the latest version of llvm binary for Windows and MinGW-w64.

You can use the following code to compile your source file

clang++ -target x86_64-pc-windows-gnu test.cc -o test.exe
Ryan M
  • 18,333
  • 31
  • 67
  • 74
jdhao
  • 24,001
  • 18
  • 134
  • 273
1

With Clang for Windows 5.0.0 (64 Bit) (latest versions available here; you want LLVM-x.y.z-win64.exe) and Visual Studio 2017 Community Edition or Build Tools installed in their default setup paths (including the latest/matching Windows SDK):

C:\>clang --version
clang version 5.0.0 (tags/RELEASE_500/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin

I've made good experiences using clang-cl (clang-cl.exe == clang.exe --driver-mode=cl) which does find all the necessary msvc library/include dependencies automatically:

C:\>clang-cl hello.cpp

Or to compile as 32 or 64 Bit application:

C:\>clang-cl -m32 hello.cpp
C:\>clang-cl -m64 hello.cpp

Alternative

See Arvid Gerstmann's Blog: Using clang on Windows.

References

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Florian
  • 39,996
  • 9
  • 133
  • 149
1

Here is what I did to use the clang compiler from the terminal on Windows 10:

  1. I downloaded and installed the Build Tools for Visual Studio 2022. This installs and opens the Visual Studio Installer.
  2. In the Visual Studio Installer I selected three things:
    1. Desktop development with C++ from the Workload tab
    2. C++ Clang Compiler for Windows (13.0.1) from the Individual Components tab.
    3. C++ Clang-cl for v143 build tools (x64/x86) from the Individual Components tab.
  3. Then I added the path to clang.exe and clang++.exe to my PATH environment variable. For me the path was C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\Llvm\x64\bin.

After doing this, I was able to use the clang compiler from the terminal. To compile a C program, go to the source directory and type;

clang *.c

To compile a C++ program, use instead;

clang++ *.cpp

I hope this information is useful to others.

robino16
  • 128
  • 6