简体   繁体   English

如何在 Windows 上从命令行运行 clang?

[英]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

VC++ Build Tools VC++ 构建工具

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,请注意您已经安装了这些。

Environment Variables环境变量

You need to tell Clang where to find the build tools and its libraries.您需要告诉 Clang 在哪里可以找到构建工具及其库。

Option 1 (vcvarsall.bat)选项 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 .amd64替换为 Clang 上的目标架构,可能是x86amd64arm 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 了。

Option 2 (Manually)选项 2(手动)

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中。

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.这一切都很少记录,因此要求可能随时更改,但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 .只需为 WindowsMinGW-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编译器所做的:

  1. I downloaded and installed the Build Tools for Visual Studio 2022 .我下载并安装了Visual Studio 2022 的构建工具 This installs and opens the Visual Studio Installer .这将安装并打开Visual Studio 安装程序
  2. In the Visual Studio Installer I selected three things:Visual Studio 安装程序中,我选择了三件事:
    1. Desktop development with C++ from the Workload tab使用工作负载选项卡中的C++ 进行桌面开发
    2. C++ Clang Compiler for Windows (13.0.1) from the Individual Components tab.单个组件选项卡中的C++ Clang Compiler for Windows (13.0.1)
    3. C++ Clang-cl for v143 build tools (x64/x86) from the Individual Components tab.用于 v143 构建工具 (x64/x86) 的 C++ Clang-cl来自单个组件选项卡。
  3. Then I added the path to clang.exe and clang++.exe to my PATH environment variable.然后我将clang.execlang++.exe的路径添加到我的 PATH 环境变量中。 For me the path was C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\Llvm\x64\bin .对我来说,路径是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.完成此操作后,我可以从终端使用 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.

相关问题 如何从Windows中的命令行运行Qt - How to run Qt from command line in windows 如何从命令行(Windows 7)编译和运行项目? - How can I compile and run project from command line(Windows 7)? 在 windows 中配置 CMake 以使用 clang 从命令行获得现代 OpenMP 支持 - Configure CMake in windows to use clang from command line to get modern OpenMP support 如何使用c ++ / c从Windows命令行一次运行多个命令? - How can I run multiple commands at once from windows command line using c++ / c? 如何在Windows上使用命令行运行UnitTest ++ - How Do I run UnitTest++ using the command line on Windows 如何让Clang Static Analyzer从命令行输出? - How to make the Clang Static Analyzer output its working from command line? 如何在 Android/NDK 上将命令行参数从 gradlew.bat 传递到 Clang - How to pass command line arguments from gradlew.bat to Clang on Android/NDK 从命令行自动应用Clang的fix-it提示 - Apply Clang's fix-it hints automatically from command line 从命令行使用clang编译:兼容性问题mac os X. - compile with clang from the command line: compatibility issues mac os X 如何在Clang Static Analyzer中污染命令行参数 - How to taint the command line arguments in Clang Static Analyzer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM