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?
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.
With Clang for Windows 5.0.0 (64 Bit) 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
- How do I tell CMake to use Clang on Windows?
- Building a x86 application with CMake, Ninja and Clang on x64 Windows
- How do I tell CMake to use Clang on Windows?
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