C++ 如何在 Windows 上编译 Clang
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9427356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compile Clang on Windows
提问by Seth Carnegie
I have been trying to find a way to get Clang working on Windows but am having trouble. I get Clang to compile successfully, but when I try to compile a program I have a bunch of errors in the standard headers.
我一直试图找到一种方法让 Clang 在 Windows 上工作,但遇到了麻烦。我让 Clang 编译成功,但是当我尝试编译一个程序时,我在标准头文件中有一堆错误。
I am aware of rubenvb's excellent prebuilt versions of clang, but I want to compile it for myself. I also was listening to the GoingNative talks about clang which said that it didn't have very good Windows support yet. How can I get clang working on Windows?
我知道rubenvb 的优秀预构建版本 clang,但我想为自己编译它。我还在听 GoingNative 关于 clang 的演讲,其中说它还没有很好的 Windows 支持。如何在 Windows 上使用 clang?
回答by Seth Carnegie
I used the following method to compile clang for C++ on Windows 7 and it has been validated by Mysticial and others:
我使用以下方法在 Windows 7 上为 C++ 编译 clang,并且它已被 Mysticial 和其他人验证:
- Download and install MinGW(make sure you install the C++ compiler) and put the bin folder in your PATH (I have MinGW 4.6.1 and tested successfully on another computer with 4.6.2)
- Make sure you have Pythonin your PATH (not 3, I have 2.7)
- (Optional: Make sure you have Perl in your PATH (I used ActivePerl 5.14.2 64-bit))
- Get CMakeand put it in your PATH
- Go to the LLVM downloads pageand download the LLVM 3.0 source code along with the Clang source code. Don't get the code from the SVN, it doesn't work with the MinGW headers.
- Extract the source codes; I had the llvm source in a folder named llvm-3.0.src on my desktop
- Put the clang source directly in a folder called "clang" (it must be called this exactly or you will build llvm but clang won't get built) in the "tools" folder inside the llvm source folder, this should make your directories look like:
- llvm source
- autoconf folder
- ...
- tools folder
- ...
- clang folder
- bindings folder
- ...
- Makefile file
- ...
- ...
- ...
- llvm source
- Make a folder named "build" in the same directory as the llvm source folder
- Open a command line and cd into the build folder
Run the command
cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release ..\llvm-3.0.src
(the last argument is the relative path to the folder that has the llvm source in it (and the clang source in the tools/clang subdirectory))
This will do the equivalent of a "configure" command, and the makefiles and everything will be generated in the build folder
- This will take a few minutes
Run the command
mingw32-make
- This will compile llvm and clang, and the clang executables will be generated in the build/bin folder
- This will probably take a long time. (You can try to speed it up by adding parallel builds,
-j<number>
option) It might be good to close all other programs so that your computer can concentrate, and so they don't interfere with the lengthy compilation process, such as putting a lock on a folder that the compiler is writing to (it happened to me). I even turned off my antivirus and firewall software so that they wouldn't try to scan the generated files and get in the way.
- 下载并安装MinGW(确保安装 C++ 编译器)并将 bin 文件夹放在您的 PATH 中(我有 MinGW 4.6.1 并在另一台具有 4.6.2 的计算机上成功测试)
- 确保你的 PATH 中有Python(不是 3,我有 2.7)
- (可选:确保您的 PATH 中有 Perl(我使用的是 ActivePerl 5.14.2 64 位))
- 获取CMake并将其放入您的 PATH
- 转到LLVM 下载页面并下载 LLVM 3.0 源代码和 Clang 源代码。不要从 SVN 获取代码,它不适用于 MinGW 标头。
- 提取源代码;我在桌面上名为 llvm-3.0.src 的文件夹中有 llvm 源
- 将 clang 源代码直接放在 llvm 源文件夹内的“tools”文件夹中的名为“clang”的文件夹中(必须准确地调用它,否则您将构建 llvm 但不会构建 clang),这应该使您的目录看起来喜欢:
- llvm 源
- 自动配置文件夹
- ...
- 工具文件夹
- ...
- 叮当文件夹
- 绑定文件夹
- ...
- 生成文件
- ...
- ...
- ...
- llvm 源
- 在与llvm源文件夹相同的目录中创建一个名为“build”的文件夹
- 打开命令行并 cd 进入 build 文件夹
运行命令
cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release ..\llvm-3.0.src
(最后一个参数是包含 llvm 源的文件夹的相对路径(以及 tools/clang 子目录中的 clang 源))
这将执行相当于“配置”命令,并且生成文件和所有内容都将在构建文件夹中生成
- 这将需要几分钟
运行命令
mingw32-make
- 这将编译 llvm 和 clang,并且将在 build/bin 文件夹中生成 clang 可执行文件
- 这可能需要很长时间。(您可以尝试通过添加并行构建来加快速度,
-j<number>
选项)关闭所有其他程序可能会很好,以便您的计算机可以集中精力,并且不会干扰冗长的编译过程,例如锁定编译器正在写入的文件夹(它发生在我身上)。我什至关闭了我的防病毒和防火墙软件,这样他们就不会尝试扫描生成的文件并妨碍他们。
Time for testing it out
测试它的时间
Create a .cpp file in the build/bin folder (I will use hello.cpp). Use a standard library header to make sure the include paths and libraries are working. Start with a very simple program.
(What I started with:
#include <iostream> int main() { std::cout << "hi"; }
)
Run the command
clang hello.cpp -std=c++0x -I"C:\MinGW\lib\gcc\mingw32\4.6.1\include\c++" -I"C:\MinGW\lib\gcc\mingw32\4.6.1\include\c++\mingw32" -Lc:/mingw/bin/../lib/gcc/mingw32/4.6.1 -Lc:/mingw/bin/../lib/gcc -Lc:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/lib -Lc:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../.. -L/mingw/lib -lstdc++ -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt
(-L specifies a directory in which to search for libraries and -l specifies a library to link) (If you do not have MinGW installed to the same path as I did, you can find out the paths with the command "g++ somefile.cpp -v" to get g++ to spill its guts about what options it is using for the library paths and library files and everything else Search near the end of the output for the -L and -l options. Be aware of the .o file names that are interspersed with the -L's. Clang uses many of the same options as g++ so I literally copied and pasted that line from the output of g++)
This should compile your program and produce a file named a.out
rename a.out to a.exe or whatever
- Run the .exe
- Your program should run.
在 build/bin 文件夹中创建一个 .cpp 文件(我将使用 hello.cpp)。使用标准库标头确保包含路径和库正常工作。从一个非常简单的程序开始。
(我从什么开始:
#include <iostream> int main() { std::cout << "hi"; }
)
运行命令
clang hello.cpp -std=c++0x -I"C:\MinGW\lib\gcc\mingw32\4.6.1\include\c++" -I"C:\MinGW\lib\gcc\mingw32\4.6.1\include\c++\mingw32" -Lc:/mingw/bin/../lib/gcc/mingw32/4.6.1 -Lc:/mingw/bin/../lib/gcc -Lc:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/lib -Lc:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../.. -L/mingw/lib -lstdc++ -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt
(-L 指定搜索库的目录,-l 指定要链接的库)(如果您没有像我一样将 MinGW 安装到相同的路径,您可以使用命令“g++ somefile.txt”找到路径。 cpp -v" 以让 g++ 详细说明它用于库路径和库文件以及其他所有内容的选项 在输出末尾附近搜索 -L 和 -l 选项。注意 .o 文件散布着 -L 的名称。Clang 使用许多与 g++ 相同的选项,所以我从字面上复制并粘贴了 g++ 输出中的那一行)
这应该编译您的程序并生成一个名为 a.out 的文件
将 a.out 重命名为 a.exe 或其他
- 运行.exe
- 你的程序应该运行。
Clang (3.0) still has some problems on Windows (I don't know if these problems are also on linux). For example, compiling a lambda (which clang doesn't support) with -std=c++0x will cause clang to crash and emit a diagnostic error. (I was informed on the LLVM IRC that this is because clang implements parsing for lambdas but not semantic analysis, which is the phase in which it crashes (because they forgot to disable parsing lambdas for the 3.0 release), and they already know about this bug)
Clang(3.0)在windows上还是有一些问题(不知道linux上是不是也有这些问题)。例如,使用 -std=c++0x 编译 lambda(clang 不支持)将导致 clang 崩溃并发出诊断错误。(我在 LLVM IRC 上得知,这是因为 clang 实现了 lambda 的解析,而不是语义分析,这是它崩溃的阶段(因为他们忘记禁用 3.0 版本的解析 lambda),而且他们已经知道这一点漏洞)
Also, the illustrious Mysticial kindly agreed to test this guide and made some observations during his testing:
此外,杰出的神秘主义者好心地同意测试本指南,并在测试过程中进行了一些观察:
- Windows headers seem to work.
- Currently only works for 32-bit.
- 64-bit compiles fine, but won't assemble.
- SSE probably is fine. ([Mysticial hasn't] tested a working SSE on 32-bit though.)
- Windows 标头似乎有效。
- 目前仅适用于 32 位。
- 64 位编译正常,但不会汇编。
- SSE可能没问题。([Mysticial 还没有] 在 32 位上测试了一个有效的 SSE。)
回答by Max Galkin
Here is what workedin my environment, on Windows 8.1, overall similar to Seth's instruction, but with fresher tools.
这里是什么工作在我的环境中,在Windows 8.1,整体类似于Seth的指令,但新鲜的工具。
- I installed MinGW 64 into
C:/MinGW
, to be precise I've used STL's distro. - I installed Python 3, just took their latest version.
- I installed CMake 3.0.2
- I forked LLVMand Clangon Github and cloned them to my machine, placing Clang's repo into llvm\tools\clang folder (the structure is described on the official page, they just show examples with svn instead of git).
- I created "build" folder next to "llvm" folder, and inside the "build" folder ran this command:
cmake -G "MinGW Makefiles" -D"CMAKE_MAKE_PROGRAM:FILEPATH=C:/MinGW/bin/make.exe" -DCMAKE_BUILD_TYPE=Release ..\llvm
(for some reason CMake couldn't find the "make" automatically) - Then I ran "make" to actually build. The build took a couple of hours.
- After that in another folder I've created 1.cpp, which executes a lambda expression to print "hi":
- 我安装MinGW的64到
C:/MinGW
,准确地说我用STL的发行。 - 我安装了 Python 3,只是采用了他们的最新版本。
- 我安装了CMake 3.0.2
- 我在 Github 上fork LLVM和Clang并将它们克隆到我的机器上,将 Clang 的 repo 放入 llvm\tools\clang 文件夹(官方页面上描述了结构,他们只是用 svn 而不是 git 显示示例)。
- 我在“llvm”文件夹旁边创建了“build”文件夹,并在“build”文件夹中运行了这个命令:(
cmake -G "MinGW Makefiles" -D"CMAKE_MAKE_PROGRAM:FILEPATH=C:/MinGW/bin/make.exe" -DCMAKE_BUILD_TYPE=Release ..\llvm
由于某种原因,CMake无法自动找到“make”) - 然后我运行“make”来实际构建。构建花了几个小时。
- 之后在另一个文件夹中我创建了 1.cpp,它执行一个 lambda 表达式来打印“hi”:
#include <iostream> int main() { []{ std::cout << "hi"; }(); }
#include <iostream> int main() { []{ std::cout << "hi"; }(); }
- I've also created a cmd file to compile the cpp. Alternatively you can just set the PATH variable properly via other means. Note that GCC vesrion in your MinGW package may be different. Also Clang has some builtin paths it tries, but they are tied to specific GCC versions, and mine was not among them, so I had to provide the include paths explicitly.
- 我还创建了一个 cmd 文件来编译 cpp。或者,您可以通过其他方式正确设置 PATH 变量。请注意,您的 MinGW 包中的 GCC 版本可能不同。Clang 也有一些它尝试的内置路径,但它们与特定的 GCC 版本相关联,而我的不在其中,所以我必须明确提供包含路径。
set PATH=<path to the build folder from step 5>/bin;c:/mingw/bin;%PATH% clang++ -std=c++11 1.cpp -o 1.exe -I"C:/MinGW/include" -I"C:/MinGW/include/c++/4.9.1" -I"C:\MinGW\include\c++.9.1\x86_64-w64-mingw32" -I"C:\MinGW\x86_64-w64-mingw32\include"
set PATH=<path to the build folder from step 5>/bin;c:/mingw/bin;%PATH% clang++ -std=c++11 1.cpp -o 1.exe -I"C:/MinGW/include" -I"C:/MinGW/include/c++/4.9.1" -I"C:\MinGW\include\c++.9.1\x86_64-w64-mingw32" -I"C:\MinGW\x86_64-w64-mingw32\include"
- Running that cmd compiled 1.cpp into 1.exe that printed "hi".
- 运行该 cmd 将 1.cpp 编译为打印“hi”的 1.exe。
What did not work:
什么不起作用:
- I've tried building the same llvm+clang sources without MinGW+GCC using the MSVC compiler from VS 2015 CTP. It built Clang successfully, the only difference is that you need to do that from the Developer CMD window, and you'd need to run
cmake -G "Visual Studio 12" ..\llvm
and then compile the solution in Visual Studio. However, that Clang failed to compile a sample cpp, it complained about "undeclared identifier 'char16_t'" and "__int128 is not supported on this target" within the MinGW standard library headers. If I use clang-cl and MS STL headers it complains about "throw x(y)" specifiers . Maybe I needed to provide some extra keys to the build, but I couldn't get it to work.
- 我已经尝试使用来自 VS 2015 CTP 的 MSVC 编译器在没有 MinGW+GCC 的情况下构建相同的 llvm+clang 源代码。它成功构建了 Clang,唯一的区别是您需要从 Developer CMD 窗口执行此操作,并且您需要
cmake -G "Visual Studio 12" ..\llvm
在 Visual Studio 中运行 然后编译解决方案。但是,Clang 未能编译示例 cpp,它在 MinGW 标准库标头中抱怨“未声明的标识符 'char16_t'”和“此目标不支持 __int128”。如果我使用 clang-cl 和 MS STL 标头,它会抱怨“throw x(y)”说明符。也许我需要为构建提供一些额外的密钥,但我无法让它工作。
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xiosbase(293,4) : error: cannot compile this throw expression yet _THROW_NCEE(failure, "ios_base::eofbit set"); C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xstddef(56,30) : note: expanded from macro '_THROW_NCEE' #define _THROW_NCEE(x, y) throw x(y)
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xiosbase(293,4):错误:无法编译此抛出表达式_THROW_NCEE(失败,“ios_base::eofbit set”);C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xstddef(56,30) :注意:扩展自宏 '_THROW_NCEE' #define _THROW_NCEE(x, y) throw x(y)
回答by Chawathe Vipul S
Refer http://clang.llvm.org/get_started.html#buildWindows
参考http://clang.llvm.org/get_started.html#buildWindows
I've used "Visual Studio 11 Win64" with cmake and its worked with the currently available VS Express for Desktop.
我已经将“Visual Studio 11 Win64”与 cmake 一起使用,并且它与当前可用的 VS Express for Desktop 一起使用。
Also for lib I'm using MinGW-W 64and for missing files SUA. http://mingw-w64.sourceforge.net/and http://www.suacommunity.com/
同样对于 lib,我使用的是 MinGW-W 64和丢失的文件 SUA。http://mingw-w64.sourceforge.net/和http://www.suacommunity.com/
For linking .o compiled by clang++ for use with the W 64binaries, I use -m i386pep with ld linker again shipped within the W 64deliverable.
为了链接由 clang++ 编译的 .o 与 W 64二进制文件一起使用,我使用 -m i386pep 和 ld 链接器再次在 W 64交付中提供。
回答by Jacob L?rfors
I had numerous problems building LLVM and clang using VS and being a Unix user I prefer building sources from the command line.
我在使用 VS 构建 LLVM 和 clang 时遇到了很多问题,作为 Unix 用户,我更喜欢从命令行构建源代码。
Following the instructions from Seth Carnegie I built it from the SVN repository as opposed to the supplied packages, using LLVM v3.3 with MinGW v4.5.2.
按照 Seth Carnegie 的说明,我从 SVN 存储库而不是提供的包构建它,使用 LLVM v3.3 和 MinGW v4.5.2。