[英]How run clang from command line on Windows?
At the Going Native conference last week, Chandler Carruth announced the existence of prebuilt binaries for running clang on windows.在上周的 Going Native 会议上,Chandler Carruth 宣布存在用于在 Windows 上运行 clang 的预构建二进制文件。 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.
目标受众是 Visual Studio 的用户,但我想从命令行运行 clang。
I ran the installer and added the LLVM bin
directory to my path, but when I try to compile "Hello world", I get this:我运行了安装程序并将 LLVM
bin
目录添加到我的路径中,但是当我尝试编译“Hello world”时,我得到了这个:
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.我找不到任何关于如何配置东西以在 Windows 上运行 clang 的信息,我猜在我弄清楚如何告诉 clang 在哪里搜索标准库头文件之后,我必须告诉它在哪里看用于链接的库。 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.
鉴于这是在 Windows 上尝试 Clang 时的常见问题,它值得一个更新的答案。
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.截至 2017 年,使用适用于 Windows 的 LLVM 3.9.1 构建,您需要以下内容才能从您的 shell 调用
clang
。
We still do not have a libc++ port for Windows, so Clang uses the VC++ libraries as well as the VC++ linker.我们仍然没有适用于 Windows 的 libc++ 端口,因此 Clang 使用 VC++ 库以及 VC++ 链接器。
So first of all you need the VC++ Build Tools on your system.所以首先你需要在你的系统上安装VC++ Build Tools 。 Do note you already have those installed if you happen to have the Visual C++ IDE.
如果您碰巧有 Visual C++ IDE,请注意您已经安装了这些。
You need to tell Clang where to find the build tools and its libraries.您需要告诉 Clang 在哪里可以找到构建工具及其库。
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
.将
amd64
替换为 Clang 上的目标架构,可能是x86
、 amd64
或arm
。 You may replace %VS140COMNTOOLS%
as well if you have a different version of the VC++ toolset.如果您有不同版本的 VC++ 工具集,您也可以替换
%VS140COMNTOOLS%
。
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.作为快捷方式,您可以运行 Visual C++ 命令提示符而不是
cmd+vcvarsall
,因为您需要为打开的每个命令提示符调用此批处理。
Now you are able to enjoy Clang.现在您可以享受Clang 了。
In case you cannot run vcvarsall.bat
or want to automate this process, welcome, I had the same need.如果您无法运行
vcvarsall.bat
或想要自动化此过程,欢迎您,我也有同样的需要。
All of the following environments variables are set automatically by vcvarsall.bat
, so you can run that and take your machine values from there.以下所有环境变量都是由
vcvarsall.bat
自动设置的,因此您可以运行它并从那里获取您的机器值。 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;
将
INCLUDE
环境变量设置为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;
将
LIB
设置为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.对于构建工具,您可以在
PATH
上使用这些工具,也可以设置VCINSTALLDIR
环境变量。 Clang will try both, favoring VCINSTALLDIR
. Clang 会同时尝试这两种方法,支持
VCINSTALLDIR
。
Set VCINSTALLDIR
to %VS140COMNTOOLS%../../VC
or add %VS140COMNTOOLS%../../VC/bin/amd64
to your PATH
.将
VCINSTALLDIR
设置为%VS140COMNTOOLS%../../VC
或将%VS140COMNTOOLS%../../VC/bin/amd64
添加到您的PATH
中。
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.这一切都很少记录,因此要求可能随时更改,但Clang MSVC 驱动程序正在尝试通过查询 Windows 寄存器和许多其他技巧来尽可能地自动化这一点,因此这些可能不再是必要的了未来。
If you are not restricted to use Microsoft compilers.如果您不限于使用 Microsoft 编译器。 You can use clang with MinGW-w64.
您可以将 clang 与 MinGW-w64 一起使用。 Just install the latest version of llvm binary for Windows and MinGW-w64 .
只需为 Windows和MinGW-w64安装最新版本的 llvm 二进制文件。
You can use the following code to compile your source file您可以使用以下代码来编译您的源文件
clang++ -target x86_64-pc-windows-gnu test.cc -o test.exe
With Clang for Windows 5.0.0 (64 Bit) (latest versions available here ; you want LLVM-xyz-win64.exe
) and Visual Studio 2017 Community Edition or Build Tools installed in their default setup paths (including the latest/matching Windows SDK):使用 Clang for Windows 5.0.0(64 位)(此处提供最新版本;您希望
LLVM-xyz-win64.exe
)和 Visual Studio 2017 社区版或构建工具安装在其默认设置路径中(包括最新/匹配的 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:我使用
clang-cl
( clang-cl.exe
== clang.exe --driver-mode=cl
) 获得了很好的体验,它确实会自动找到所有必要的msvc
库/包含依赖项:
C:\>clang-cl hello.cpp
Or to compile as 32 or 64 Bit application:或编译为 32 或 64 位应用程序:
C:\>clang-cl -m32 hello.cpp
C:\>clang-cl -m64 hello.cpp
Alternative选择
See Arvid Gerstmann's Blog: Using clang on Windows .请参阅Arvid Gerstmann 的博客:在 Windows 上使用 clang 。
References参考
Here is what I did to use the clang compiler from the terminal on Windows 10:这是我在 Windows 10 的终端上使用clang编译器所做的:
After doing this, I was able to use the clang compiler from the terminal.完成此操作后,我可以从终端使用 clang 编译器。 To compile a C program, go to the source directory and type;
要编译 C 程序,请转到源目录并键入;
clang *.c
To compile a C++ program, use instead;要编译 C++ 程序,请改用;
clang++ *.cpp
I hope this information is useful to others.我希望这些信息对其他人有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.