Compile with clang 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.
Читайте также:  Папка efi mac os

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:

Setting up the Clang Compiler in CLion on Windows

With CMake 3.15 it has become possible to use the Clang compiler on Windows with a GNU-style command line. This also means that it’s possible to use Clang with the Mingw-w64 toolchain.

While it’s possible to configure Clang with the Mingw-w64 (or MinGW) toolchain, if you decide to use Clang from the LLVM repo, it won’t work correctly. This is because LLVM Clang for Windows is built using Microsoft Visual Studio, and all the built-in macros and include search paths are set up for use with Visual Studio. So even when used with the MinGW toolchain, it will still try to include MSVC standard library headers.

We’ve done some experiments and found a possible workflow that involves using CLion in combination with the MinGW toolchain and Clang compiler.

Here are our findings:

  1. Install MSYS2 from https://www.msys2.org, follow their installation guide.
  2. Get the necessary packages with the pacman tool. We installed the following (pacman -S):
  • mingw-w64-x86_64-gcc
  • mingw-w64-x86_64-clang
  • mingw-w64-x86_64-lld
  • mingw-w64-x86_64-gdb
  • mingw-w64-x86_64-make
  • mingw-w64-x86_64-polly
  • mingw-w64-x86_64-compiler-rt

This clang compiler is built with mingw-w64 and has paths and macros that correspond to this toolchain.

Now we are ready to set up the CLion toolchain. Go to Settings/Preferences | Build, Execution, Deployment | Toolchains:

With your new toolchain configured, you can start building your project. You can use either the default ld linker or set the lld with -DCMAKE_LINKER=lld .

Using advanced tools provided by the Clang compiler

In theory, all Clang tools should also work without any issues. However, problems can arise involving compiler-rt. compiler-rt is a set of runtime libraries which are required to use sanitizers and profiling in Clang, the current compiler_rt package was built with MinGW. But Clang requires a compiler that is built with Clang and lld.

In our case, we wanted to use profile-guided optimization. One of the options for making this work is to get the compiler-rt source code of exactly the same version as the Clang version in MSYS2. This can be challenging, so the other solution is to clone the LLVM monorepo and build the tools you need.

For -fprofile-instr-generate it might be enough to build only compiler-rt and llvm-profdata to merge the profiler results. However, to use all the tools reliably, it’s better to build Clang and lld, as well.

Luckily we already have the required setup for that build.

The last step is to replace the binaries in \mingw64\lib\clang\ \lib\windows with libraries from \cmake-build-release-mingw_clang\lib\windows or from \lib\clang\ \lib\windows in case of monorepo.

Using Clang for profiling

With the compiler-rt libraries in place, it’s now possible to use the same toolchain we set up with -fprofile-instr-generate/-fprofile-instr-use flags. Let’s build LLVM for this experiment, since we already have the source code. We’ll also use -DLLVM_ENABLE_LTO=Thin for even more optimization. Go to Settings/Preferences | Build, Execution, Deployment | CMake:

With this CMake configuration, you can build the Clang compiler and use it, for example, to build your own project. This will generate the relevant profiler information, which should later be merged with the tool we built earlier – llvm-profdata. With the merged profile_merged.profdata file, you can finally build the optimized version of the Clang compiler:

Читайте также:  Как найти c programdata microsoft windows start menu programs

Using custom Clang and lld

Getting the gcc-style -fprofile-generate/-fprofile-use flags to work correctly requires changing the Clang path and setting -DCMAKE_LINKER to the newly built lld. You also need a few additional tricks for LLVM : -femulated-tls and linking pthread.

Then you should repeat all the steps you performed with -fprofile-instr-generate/-fprofile-instr-use.

Conclusion

Using Clang on Windows is now possible and does not require you to have Microsoft Visual Studio installed!

We hope that in the near future, using advanced clang tools will become easier and will no longer require the manual build. Let us know if you discover any other ways to achieve the same results!

Your CLion team
JetBrains
The Drive to Develop

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 live mail не работает после обновления windows 10 2004

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.)
    Оцените статью