在Windows下配置Clang编译器
Preferences
- Linux & macOS 平台LLVM 相关工具链下载
- 2019年,在Windows下配置Clang编译器
- Visual Studio 2022 中使用 Clang
- clion使用clang编译
- Clion 2020.3:如何设置Clang编译器
这篇文章主要介绍如何在Windows使用Clang编译器来编译C/C++程序(在命令行下,clang是C编译器,编译C++需要使用clang++)。
简介
基于LLVM强大的模块性和优化能力,作为C/C++编译器的Clang后发优势惊人。
- Firefox在所有平台上都是用Clang编译了:Firefox is now built with clang LTO on all* platforms
- Chrome在Windows上也使用Clang来编译:Clang is now used to build Chrome for Windows
Windows 安装 Clang 预编译好的二进制包
在 LLVM Download Page 页面可以下载预编译好的 Clang Windows 包(目前最新的包是17.01)。这个包里面内容还挺全的,包含Clang Tools和Extra Clang Tools。
手动编译
如果你不想使用预先编译好的包,比如你想对编译选项做一些调整,可以手动编译Clang。可以参考下面文档:
- https://llvm.org/docs/CMake.html
- https://clang.llvm.org/get_started.html
- Getting Started with the LLVM System using Microsoft Visual Studio¶
有几个关键步骤在这里阐述一下:
到
下载源代码:
- cfe-7.0.1.src.tar.xz
- clang-tools-extra-7.0.1.src.tar.xz
- llvm-7.0.1.src.tar.xz
把上面的源代码解压后放在正确的目录下:
- llvm-7.0.1.src.tar.xz 解压并重命名成llvm
- cfe-7.0.1.src.tar.xz解压llvm/tools目录下,并重命名成clang
- clang-tools-extra-7.0.1.src.tar.xz解压到llvm/tools/clang/tools目录下,并重命名成extra
安装Visual Studio 2017 (Community版即可)
安装CMake和GnuWin32Utils
在llvm目录下创建一个build目录,并进入
打开命令行提示符(确保CMake和GnuWin32Utils都在PATH中),执行
cmake -G "Visual Studio 15 2017" -A x64 -Thost=x64 ..用Visual Studio打开LLVM.sln,设置目标项目为ALL_BUILD,配置类型为Release,然后开始构建。睡个午觉之后,差不多就编译好了,生成的文件在build/Release目录下
C++标准库
Clang的C++标准库[libc++](https://libcxx.llvm.org/)看起来不支持Winodws,所以用clang++编译下面这个C++程序的时候:
#include <iostream>
using namespace std;
int main() {
cout << "Hello!" << endl;
return 0;
}
会显示错误:
clang++.exe: warning: unable to find a Visual Studio installation; try running C
lang from a developer command prompt [-Wmsvc-not-found]
a.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^~~~~~~~~~
1 error generated.
解决这个问题有两个办法,一个是使用Visual Studio提供的C++库,另一个是使用MinGW提供的GCC的C++标准库(libstdc++)。
使用Visual Studio的C++库
这是Clang的默认选项。
执行clang -v可以看到:
clang version 17.0.1
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: D:\Soft\Language\LLVM\bin
默认的Target是x86_64-pc-windows-msvc,也就是使用isual Studio的C++标准库。
如果你安装了全套的Visual Studio (建议 VS2017之后的版本),那么从开始菜单的Visual Studio目录下打开Visual Studio的命令行,在这个命令行里面使用clang编译C++,clang会自己找到相应的C++库。
如果你没有安装全套的Visual Studio也不要紧,其实所需要的只是Visual Studio Build Tools,可以在VisualStudio Download下面。 关于如何使用Build Tools,可以参照这篇文章 How to compile C++ code with VS Code and Clang or Windows Tools | How To Install VS Microsoft C++ Build Tools on Windows。
使用Visual Studio或者其相关的工具需要受到微软的License约束,目前Visual Studio的Community版本可以用于个人或者非商业项目。如果公司项目中使用Visual Studio Community版则有点违反授权的意味。
Update:
根据https://devblogs.microsoft.com/cppblog/updates-to-visual-studio-build-tools-license-for-c-and-cpp-open-source-projects/,https://visualstudio.microsoft.com/visual-cpp-build-tools/的授权限制放宽了。
使用MinGW提供libstdc++
是的,Clang可以从GCC那借用C++标准库,也就是libstdc++。在Windows上MinGW项目提供了一个Windows版的GCC,包含libstd++可供Clang使用。
在MinGW的下载页面可以看到很多下载选项,适合Windows的有Cygwin、MingW-W64-builds和Msys2。
没什么别的需求的话,可以选用MingW-W64-builds。
Update:
可以从下面地址获取较新的基于MinGW的GCC:
MingW-W64-builds提供一个安装器,来帮你选择合适的编译版本。最主要的是要选好i686版本,还是x86_64版本。本文以x86_64为例。
要让Clang使用MinGW,需要为clang指定命令行选项-target x86_64-pc-windows-gnu,但是我们执行clang++ -target x86_64-pc-windows-gnu a.cpp发现a.cpp:1:10: fatal error: 'iostream' file not found的错误依然存在。
这主要是因为MinGW默认安装在C:\Program Files\mingw-w64下面,Clang找不到MinGW。使用额外的-v选项,我们可以发现:
clang -cc1 version 7.0.1 based upon LLVM 7.0.1 default target x86_64-pc-win32
ignoring nonexistent directory "C:\Program Files\LLVM\x86_64-w64-mingw32\include\c++"
ignoring nonexistent directory "C:\Program Files\LLVM\x86_64-w64-mingw32\include\c++\x86_64-w64-mingw32"
ignoring nonexistent directory "C:\Program Files\LLVM\x86_64-w64-mingw32\include\c++\backward"
ignoring nonexistent directory "C:\Program Files\LLVM\x86_64-w64-mingw32\include\c++\"
ignoring nonexistent directory "C:\Program Files\LLVM\x86_64-w64-mingw32\include\c++\\x86_64-w64-mingw32"
ignoring nonexistent directory "C:\Program Files\LLVM\x86_64-w64-mingw32\include\c++\\backward"
ignoring nonexistent directory "C:\Program Files\LLVM\include\c++\"
ignoring nonexistent directory "C:\Program Files\LLVM\include\c++\\x86_64-w64-mingw32"
ignoring nonexistent directory "C:\Program Files\LLVM\include\c++\\backward"
ignoring nonexistent directory "include\c++"
ignoring nonexistent directory "include\c++\x86_64-w64-mingw32"
ignoring nonexistent directory "include\c++\backward"
ignoring nonexistent directory "C:\Program Files\LLVM\x86_64-w64-mingw32/sys-root/mingw/include"
ignoring nonexistent directory "C:\Program Files\LLVM\x86_64-w64-mingw32\include
"
#include "..." search starts here:
#include <...> search starts here:
C:\Program Files\LLVM\lib\clang\7.0.1\include
C:\Program Files\LLVM\include
End of search list.
a.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^~~~~~~~~~
1 error generated.
Clang默认在自己的安装目录C:\Program Files\LLVM下查找MinGW。要解决这个问题,一个办法是把MinGW安装或者链接到Clang需要的目录。
此外还有一个办法,就是把MingGW的g++命令添加到PATH环境变量中去。以我的MinGW安装为例,在命令行中执行
set path=C:\Program Files\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin;%path%
然后Clang就可以通过g++顺藤摸瓜,找到相应的头文件和库。
其他
- How to build the latest clang-tidy
- 解决Clang在Windows下无法使用的问题
- 在VS Code中使用Clang作为你的C++编译器
- Homebrew LLVM receipe
- How to use clang with mingw-w64 headers on windows
- LLVM MinGW
- Clang C++ Cross Compiler - Generating Windows Executable from Mac OS X
- Clang+llvm windows运行环境配置
- Installing Clang 3.5 for Windows
- Home / Guide to predefined macros in C++ compilers (gcc, clang, msvc etc.) edit
- How to compile C++ for Windows with clang in Visual Studio 2015
- HackerNews: mingw-w64 llvm maintainer/developer here.
- Setting up Clang on Windows
- General notes on compiler support
- How To Cross-Compile Clang/LLVM using Clang/LLVM
- build llvm again
- https://github.com/martell/mingw-w64-clang
- https://github.com/mstorsjo/llvm-mingw
- https://github.com/tpoechtrager/wclang
- http://blog.johannesmp.com/2015/09/01/installing-clang-on-windows-pt1/
- https://stackoverflow.com/questions/32239122/what-do-you-need-to-install-to-use-clang-on-windows-to-build-c14-for-64-bit
- https://stackoverflow.com/questions/14023696/how-to-set-up-clang-to-use-mingw-libstdc
- https://www.reddit.com/r/cpp/comments/3pe6j9/why_should_i_use_clang_over_g/
在Windows下配置Clang编译器的更多相关文章
- QT + OpenCV + MinGW 在windows下配置开发环境
由于研究项目需要,最近开始接触C++界面设计,关于“QT + OpenCV + MinGW在windows下配置开发环境”着实让人头疼,单次配置时间相当长,也十分不容易,本人第一次配置成 ...
- windows 下配置 Nginx 常见问题(转)
windows 下配置 Nginx 常见问题 因为最近的项目需要用到负载均衡,不用考虑,当然用大名鼎鼎的Nginx啦.至于Nginx的介绍,这里就不多说了,直接进入主题如何在Windows下配置. 我 ...
- Windows下配置使用 MemCached
Windows下配置使用MemCached 工具: memcached-1.2.6-win32-bin.zip MemCached服务端程序(for win) Memcached Manage ...
- windows下配置wnmp
最近尝试windows下配置nginx+php+mysql,在这里总结一下. 1.下载windows版本的nginx,官网下载地址:http://nginx.org/en/download.htm, ...
- windows下配置lamp环境(5)---配置MySQL5.6
开始配置mysql 1.创建配置文件my.ini 1.进入C:\wamp\MySQL 2.把my-default.ini 另存一份:my.ini 3.开始编辑mysql的配置文件,打开my ...
- windows下配置lamp环境(3)---配置PHP5.4
下面配置php Php文件夹里有两个php.ini-*文件,随便修改一个,去掉后缀,变成php.ini (如图) 打开php.ini ,添加php扩展目录723行左右(其实放哪都无所谓,只不过php. ...
- windows下配置lamp环境(0)---软件获取
工作快一年了,还没有怎么配置过服务器环境,经常使用集成套件wampserver,为了复习配置wamp服务器 特意在虚拟机中测试安装步骤如下. 安装前步骤:下载软件.软件下载地址如下: 1.apache ...
- windows下配置lamp环境(2)---配置Apache服务器2.2.25
配置Apache 配置Apache时,先要找到安装目录中的主配置文httpd.conf,使用文本编辑器打开,最好不要使用windows自带的编辑器,可以使用NotePad++, vim,或者subli ...
- windows下配置svn的https访问
svn是一个功能强大的代码版本管理系统,可以将服务端安装在linux.unix以及windows下.svn通常采用http方式进行代码提交与下载.由于密码采用明文传输,因此存在泄密的风险.若采用htt ...
- windows下配置lamp环境(1)---安装Apache服务器2.2.25
window下lamp成为wamp; 安装wamp环境的第一步是安装Apache服务器.下面开始安装步骤图文并茂. 一.双击安装包点“next”进行下一步,然后同意协议(这张图没有截):
随机推荐
- .NET8 Blazor的Auto渲染模式的初体验
.NET8发布后,Blazor支持四种渲染方式 静态渲染,这种页面只可显示,不提供交互,可用于网页内容展示 使用Blazor Server托管的通过Server交互方式 使用WebAssembly托管 ...
- PEP9
利用循环语句 counter 是计数器 需要在后面输入个3才是3个数字之和 Set sum to 0 Set counter to 0 Set limit to number of values to ...
- 常用【描述性统计指标】含义(by python)
统计学有时候会被误解,好像必须有大量的样本数据,才能使统计结果有意义.这会让我们觉得统计学离我们的日常生活很遥远. 其实,如果数据的准确度高的话,少量的样本数据同样能反映出真实的情况.比如,很多国家选 ...
- [AGC034D] Manhattan Max Matching
Problem Statement Snuke is playing with red and blue balls, placing them on a two-dimensional plane. ...
- [ABC265B] Explore
Problem Statement Takahashi is exploring a cave in a video game. The cave consists of $N$ rooms arra ...
- Redis 学习笔记1:数据类型
Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及 zset(sorted set:有序集合). 一.Redis 数据类型-STRING 1.常用 ...
- 组合式api-ref引用子组件、dom元素, defineExpose的使用
和vue2一样,我们有时候希望父组件能够调用子组件中的方法.属性.那么就要用到ref. 然后你会发现,根本调用不了子组件中的方法"sonSayHi",如下图: 原因: 使用
- 码农的转型之路-PLC异地组网与远程控制
PLC异地组网与远程控制,需求是基于园子认识的朋友提供,大体是实现PLC多个局域网异地组网,并实现远程控制.大屏展示.手机端控制.预警推送等功能.其他就是可以方便二次开发界面,以满足不同客户的需求. ...
- vue-admin-template动态菜单后台获取菜单
vue-admin-template.vue-element-admin配置动态菜单,菜单数据从后台获取. 我在网上search了几个小时也没有找到想要的emm,翻官网也没有说明,只说明了路由覆盖.只 ...
- JNA入门(一)
JNA入门,代码在github写得明明白白:https://github.com/java-native-access/jna/blob/master/www/GettingStarted.md 一. ...