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.
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?
I used the following method to compile clang for C++ on Windows 7 and it has been validated by Mysticial and others:
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
Run the command mingw32-make
-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. 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
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)
Also, the illustrious Mysticial kindly agreed to test this guide and made some observations during his testing:
Here is what worked in my environment, on Windows 8.1, overall similar to Seth's instruction, but with fresher tools.
C:/MinGW
, to be precise I've used STL's distro . 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) #include <iostream> int main() { []{ std::cout << "hi"; }(); }
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++\\4.9.1\\x86_64-w64-mingw32" -I"C:\\MinGW\\x86_64-w64-mingw32\\include"
What did not work :
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. 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)
Refer 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.
Also for lib I'm using MinGW-W 64 and for missing files SUA. http://mingw-w64.sourceforge.net/ and http://www.suacommunity.com/
For linking .o compiled by clang++ for use with the W 64 binaries, I use -m i386pep with ld linker again shipped within the W 64 deliverable.
I had numerous problems building LLVM and clang using VS and being a Unix user I prefer building sources from the command line.
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.
clang
on Windows
is NOT stand-alone, and it needs to be combined with another compiler (like MingW
or MSVC
).
The steps to do said combination are rather complicated, at least if you don't want to miss anything, hence the "build script" is worth an entire project:
https://github.com/mstorsjo/llvm-mingw
I have tested llvm-mingw
project's version 20220906 (at time of writting, latest version), and it combines clang 15.0.0
with MingW
seamlessly.
See pre-built binary releases:
https://github.com/mstorsjo/llvm-mingw/releasesOr try what worked for me:
https://github.com/mstorsjo/llvm-mingw/releases/download/20220906/llvm-mingw-20220906-msvcrt-i686.zip
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.