Compiling with clang on windows

Installing clang++ to compile and link on Windows : Part 2

01 Sep 2015

Part 1 : The Problem
Part 2 : Approach 1 — Clang 3.7
Part 3 : Approach 2 — MSYS2

Approach 1 — Clang 3.7

In this approach we’ll be installing Clang 3.7.0 and gcc 5.1.0 (via MinGW-w64), to allow us to build and link with clang from the standard windows command prompt.

First quickly consider the following:

  • This tutorial was created for, and tested with windows 8.1 — I can’t make promises how well it will work on windows 10.
  • If you have an older version of clang/LLVM installed, please use its uninstaller before continuing. Make sure that neither C:\Program Files\LLVM or C:\Program Files (x86)\LLVM exist on your machine.
  • You’ll need to edit your system PATH, and the standard windows UI for it is pretty awful. You should consider grabbing the free Windows Environment Variable Editor or “Eveditor”: http://eveditor.com/ which is more user friendly and shows you if you have invalid paths set.
  • You will also need to be able to open .7z archives, so make sure to install 7zip or something similar.

Ok, let’s get to the fun part. You will need to install all of the following:

  1. Clang for 64 bit
    • Download Link — clang 3.7.0 64 bit.
    • Run the installer. When you get to the PATH settings, make sure to add LLVM to the system PATH:
    • Use the default install location: C:\Program Files\LLVM . Once the installation completes hit ‘Finish’.
    • clang++.exe should be located in C:\Program Files\LLVM\bin , which should be in your system PATH.
  2. Clang for 32 bit
    • Download Link — clang 3.7.0 32 bit.
    • Run the installer. IMPORTANT: because we technically just installed a different version of LLVM, the installer will see the 64 bit version as an ‘older version’ and will give you this warning: You will want to click ‘No’, so that the 64 bit version doesn’t get uninstalled. We want both the 32 and the 64 bit versions.
    • You will once again be prompted to add LLVM to the path. This time leave it set to Do not add LLVM to the system PATH .
    • Use the default install location: C:\Program Files (x86)\LLVM and complete the installation.
    • clang++.exe should be located in C:\Program Files (x86)\LLVM\bin , but should NOT be in your system path.
  3. MinGW-w64 for 64 bit
    • Download Link — MinGW-w64: 64 bit, version 5.1.0 with posix threads and seh exceptions.
    • Extract the x86_64….7z file, either to your desktop (and move it), or directly to your C: Drive:
    • Once done you should be able to find g++.exe in C:\mingw64\bin — you cannot change this location because Clang has it hardcoded.
    • Add C:\mingw64\bin to your system PATH.
  4. MinGW-w64 for 32 bit
    • Download Link — MinGW-w64: 32 bit, version 5.1.0 with posix threads and dwarf exceptions.
    • Extract the x686….7z file, either to your desktop (and move it), or directly to your C: Drive.
    • Once done you should be able to find g++.exe in C:\mingw32\bin
    • Do NOT add mingw32 to your path!
  5. Batch files
    • The 64 bit version of clang/g++ will be used by default. If you want to use the 32 bit versions you can temporarily modify your path with these batch files:
    • Right click view raw and save setgcc32.bat and setgcc64.bat :
    • Both batch files should be in a folder on the PATH. For example, create C:\Utils , place the batch files in there and add C:\Utils to your path.

And that’s it! At this point, you should have the 32 bit and 64 bit versions of Clang 3.7.0 and MinGW-w64 5.1.0 installed. You can now compile 64 bit windows applications through clang like this:

And if you need to compile to 32 bit, just run the batch script, which will temporarily modify your path to prefer the 32 bit versions:

Note: If you see a fatal error that some common library cannot be found when you try to compile, such as:

Читайте также:  Linux просмотреть пользователей группы

How do I compile C++ with Clang?

I have installed Clang by using apt-get in Ubuntu, and I can successfully compile C files using it. However, I have no idea how to compile C++ through it. What do I need to do to compile C++?

6 Answers 6

The command clang is for C, and the command clang++ is for C++.

I do not know why there is no answer directly addressing the problem. When you want to compile C++ program, it is best to use clang++ . For example, the following works for me:

If compiled correctly, it will produce the executable file test , and you can run the file by using ./test .

Or you can just use clang++ test.cc to compile the program. It will produce a default executable file named a.out . Use ./a.out to run the file.

The whole process is a lot like g++ if you are familiar with g++. See this post to check which warnings are included with -Wall option. This page shows a list of diagnostic flags supported by Clang.

A note on using clang -x c++ : Kim Gräsman says that you can also use clang -x c++ to compile cpp programs, but that may not be true. For example, I am having a simple program below:

clang++ test.cc -o test will compile successfully, but clang -x c++ will not, showing a lot undefined references errors. So I guess they are not exactly equivalent. It is best to use clang++ instead of clang -x c++ when compiling c++ programs to avoid extra troubles.

  • clang version: 11.0.0
  • Platform: Ubuntu 16.04

How to compile Clang on Windows

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?

4 Answers 4

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

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

  • 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 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.
Читайте также:  Настройка почтовый сервер для windows

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:

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.
  • 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.)

    Getting Started: Building and Running Clang

    This page gives you the shortest path to checking out Clang and demos a few options. This should get you up and running with the minimum of muss and fuss. If you like what you see, please consider getting involved with the Clang community. If you run into problems, please file bugs in LLVM Bugzilla.

    Release Clang Versions

    Clang is released as part of regular LLVM releases. You can download the release versions from https://llvm.org/releases/.

    Clang is also provided in all major BSD or GNU/Linux distributions as part of their respective packaging systems. From Xcode 4.2, Clang is the default compiler for Mac OS X.

    Building Clang and Working with the Code

    On Unix-like Systems

    If you would like to check out and build Clang, the current procedure is as follows:

    1. Get the required tools.
      • See Getting Started with the LLVM System — Requirements.
      • Note also that Python is needed for running the test suite. Get it at: https://www.python.org/downloads/
      • Standard build process uses CMake. Get it at: https://cmake.org/download/
    2. Check out the LLVM project:
      • Change directory to where you want the llvm directory placed.
      • git clone https://github.com/llvm/llvm-project.git
    3. Build LLVM and Clang:
      • cd llvm-project
      • mkdir build (in-tree build is not supported)
      • cd build
      • cmake -DLLVM_ENABLE_PROJECTS=clang -G "Unix Makefiles" ../llvm
      • make
      • This builds both LLVM and Clang for debug mode.
      • Note: For subsequent Clang development, you can just run make clang.
      • CMake allows you to generate project files for several IDEs: Xcode, Eclipse CDT4, CodeBlocks, Qt-Creator (use the CodeBlocks generator), KDevelop3. For more details see Building LLVM with CMake page.
    4. If you intend to use Clang’s C++ support, you may need to tell it how to find your C++ standard library headers. In general, Clang will detect the best version of libstdc++ headers available and use them — it will look both for system installations of libstdc++ as well as installations adjacent to Clang itself. If your configuration fits neither of these scenarios, you can use the -DGCC_INSTALL_PREFIX cmake option to tell Clang where the gcc containing the desired libstdc++ is installed.
    5. Try it out (assuming you add llvm/build/bin to your path):
      • clang --help
      • clang file.c -fsyntax-only (check for correctness)
      • clang file.c -S -emit-llvm -o - (print out unoptimized llvm code)
      • clang file.c -S -emit-llvm -o - -O3
      • clang file.c -S -O3 -o - (output native machine code)
    6. Run the testsuite:
      • make check-clang
    Читайте также:  Oculus dk2 drivers windows 10

    Using Visual Studio

    The following details setting up for and building Clang on Windows using Visual Studio:

    1. Get the required tools:
      • Git. Source code control program. Get it from: https://git-scm.com/download
      • CMake. This is used for generating Visual Studio solution and project files. Get it from: https://cmake.org/download/
      • Visual Studio 2017 or later
      • Python. It is used to run the clang test suite. Get it from: https://www.python.org/download/
      • GnuWin32 tools The Clang and LLVM test suite use various GNU core utilities, such as grep, sed, and find. The gnuwin32 packages are the oldest and most well-tested way to get these tools. However, the MSys utilities provided by git for Windows have been known to work. Cygwin has worked in the past, but is not well tested. If you don’t already have the core utilies from some other source, get gnuwin32 from http://getgnuwin32.sourceforge.net/.
    2. Check out LLVM and Clang:
      • git clone https://github.com/llvm/llvm-project.git

    Note: Some Clang tests are sensitive to the line endings. Ensure that checking out the files does not convert LF line endings to CR+LF. If you’re using git on Windows, make sure your core.autocrlf setting is false.

  • Run CMake to generate the Visual Studio solution and project files:
    • cd ..\.. (back to where you started)
    • mkdir build (for building without polluting the source dir)
    • cd build
    • If you are using Visual Studio 2017: cmake -DLLVM_ENABLE_PROJECTS=clang -G "Visual Studio 15 2017" -A x64 -Thost=x64 ..\llvm
      -Thost=x64 is required, since the 32-bit linker will run out of memory.
    • To generate x86 binaries instead of x64, pass -A Win32.
    • See the LLVM CMake guide for more information on other configuration options for CMake.
    • The above, if successful, will have created an LLVM.sln file in the build directory.
  • Build Clang:
    • Open LLVM.sln in Visual Studio.
    • Build the «clang» project for just the compiler driver and front end, or the «ALL_BUILD» project to build everything, including tools.
  • Try it out (assuming you added llvm/debug/bin to your path). (See the running examples from above.)
  • See Hacking on clang — Testing using Visual Studio on Windows for information on running regression tests on Windows.
  • Using Ninja alongside Visual Studio

    We recommend that developers who want the fastest incremental builds use the Ninja build system. You can use the generated Visual Studio project files to edit Clang source code and generate a second build directory next to it for running the tests with these steps:

    1. Check out clang and LLVM as described above
    2. Open a developer command prompt with the appropriate environment.
      • If you open the start menu and search for «Command Prompt», you should see shortcuts created by Visual Studio to do this. To use native x64 tools, choose the one titled «x64 Native Tools Command Prompt for VS 2017».
      • Alternatively, launch a regular cmd prompt and run the appropriate vcvarsall.bat incantation. To get the 2017 x64 tools, this would be:
        "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
    3. mkdir build_ninja (or build, or use your own organization)
    4. cd build_ninja
    5. set CC=cl (necessary to force CMake to choose MSVC over mingw GCC if you have it installed)
    6. set CXX=cl
    7. cmake -GNinja ..\llvm
    8. ninja clang This will build just clang.
    9. ninja check-clang This will run the clang tests.

    Clang Compiler Driver (Drop-in Substitute for GCC)

    The clang tool is the compiler driver and front-end, which is designed to be a drop-in replacement for the gcc command. Here are some examples of how to use the high-level driver:

    The ‘clang’ driver is designed to work as closely to GCC as possible to maximize portability. The only major difference between the two is that Clang defaults to gnu99 mode while GCC defaults to gnu89 mode. If you see weird link-time errors relating to inline functions, try passing -std=gnu89 to clang.

    Examples of using Clang

    Preprocessing:

    Type checking:

    GCC options:

    Pretty printing from the AST:

    Note, the -cc1 argument indicates the compiler front-end, and not the driver, should be run. The compiler front-end has several additional Clang specific features which are not exposed through the GCC compatible driver interface.

    Оцените статью