Getting Clang to Work on Windows

Getting Clang to work on windows

There's some instructions for building clang on this page (hidden in the "Clang Development" part of the sidebar...). For MinGW you want the section called "On Unix-like Systems". The only tricky part is step 5 which tells you how to set up the paths for the C++ standard library. These need to be added into the code in clang/lib/Frontend/InitHeaderSearch.cpp. On my machine it wound up looking like this

// FIXME: temporary hack: hard-coded paths.
AddPath("/usr/local/include", System, true, false, false);
AddPath("c:/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/include/c++", System, true, false, false);
AddPath("c:/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/include/c++/mingw32", System, true, false, false);
AddPath("c:/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/include/c++/backward", System, true, false, false);
AddPath("c:/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../../include", System, true, false, false);
AddPath("c:/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/include", System, true, false, false);
AddPath("c:/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/include-fixed", System, true, false, false);

though I'm not sure all these are needed!

How clang works on Windows?

Clang is just a compiler, and a flexible one at that. The LLVM umbrella project which Clang is a part of also provides a linker (lld) and a C++ standard library (libc++) among other low-level things.

On Windows, you have two main options:

  1. Use clang-cl: compiles with a clang wrapper that understands cl.exe's optionsns. This links to the Visual C++ runtime/library and thus you really only replace the Visual Studio compiler, but not the library/runtime. The code you compile like this should be compatible with normal MSVC-compiled code.

  2. Use clang: this is the clang driver that understands GCC-like options. You would use it if you want to compile against the MinGW-w64 runtime.

Clang has an option to select its C++ standard library: -stdlib=libc++ selects the LLVM libc++. I doubt this works for clang-cl, but I haven't tried that to be honest.
If you install Clang+libc++ through MSYS2, you can compile against libc++ and the MinGW-w64 runtime using the -stdlib option.


As to your concrete issue: you seem to be using the GNU-style interface for Clang, but it's detecting you're on Windows and compiles as if it were clang-cl.
I think your best bet is to try a MinGW-w64-targeting clang (e.g. from MSYS2 as I suggested above).

As Clang is a flexible compiler, you could also specify the target with the -target commandline option. Unfortunately, I don't know the values you would need (nor the extra options to actually make it find the libc++ headers/libraries).
That being said, I'm not sure libc++ works for a Visual Studio targeting Clang at all.

How to compile Clang on Windows

I used the following method to compile clang for C++ on Windows 7 and it has been validated by Mysticial and others:

  1. 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)
  2. Make sure you have Python in your PATH (not 3, I have 2.7)
  3. (Optional: Make sure you have Perl in your PATH (I used ActivePerl 5.14.2 64-bit))
  4. Get CMake and put it in your PATH
  5. Go to the LLVM downloads page and 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.
  6. Extract the source codes; I had the llvm source in a folder named llvm-3.0.src on my desktop
  7. 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
          • ...
        • ...
      • ...
  8. Make a folder named "build" in the same directory as the llvm source folder
  9. Open a command line and cd into the build folder
  10. 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
  11. 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.

Time for testing it out

  1. 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";
    }

    )

  2. 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

  3. rename a.out to a.exe or whatever

  4. Run the .exe
  5. Your program should run.

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:

  1. Windows headers seem to work.
  2. Currently only works for 32-bit.
  3. 64-bit compiles fine, but won't assemble.
  4. SSE probably is fine. ([Mysticial hasn't] tested a working SSE on 32-bit though.)


Related Topics



Leave a reply



Submit