Clang++ with MinGW headers on Windows

Introduction

To build Konstruisto on Windows, we need to set up Clang++ compilation with MinGW headers. It’s not a complicated process, but can take some time if you don’t know where to start. Please note we are setting up only 64-bit builds here.

I assume you have some terminal emulator installed (Git Bash does the job here). Make sure you can open .7z archives (you can download 7-Zip for free).

Downloading LLVM

LLVM is a set of tools for compilation of various languages. It contains clang++. To get the latest stable version, go to their Downloads Page and choose latest stable Windows (64-bit) binaries. We use LLVM 7.0.0 in this article.

Make sure LLVM’s bin folder is in PATH, and restart terminal emulator if it was already running.

LLVM in Path

Missing headers

Let’s create clang-test.cpp. Now, clang++ will complain about missing headers:

Clang++: No MSVC installation

On Windows, it tries to find Visual Studio compiler first, and the message says it was not found. Compilation may be successful if you already have MSVC installed, but it’s not what we’re looking for. Setting target to x86_64-pc-windows-gnu says we want 64-bit binaries using gcc headers:

Clang++: No gcc headers

These are still missing, as we haven’t installed sources. Add --verbose to see where clang was looking:

kantoniak@kantoniak-razer:~ $ clang++ –verbose -target x86_64-pc-windows-gnu clang-test.cpp
clang version 7.0.0 (tags/RELEASE_700/final)
Target: x86_64-pc-windows-gnu
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
“C:\\Program Files\\LLVM\\bin\\clang++.exe” -cc1 -triple x86_64-pc-windows-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name clang-test.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -v -resource-dir “C:\\Program Files\\LLVM\\lib\\clang\\7.0.0” -internal-isystem “C:\\Program Files\\LLVM\\x86_64-w64-mingw32\\include\\c++” -internal-isystem “C:\\Program Files\\LLVM\\x86_64-w64-mingw32\\include\\c++\\x86_64-w64-mingw32” -internal-isystem “C:\\Program Files\\LLVM\\x86_64-w64-mingw32\\include\\c++\\backward” -internal-isystem “C:\\Program Files\\LLVM\\x86_64-w64-mingw32\\include\\c++\\” -internal-isystem “C:\\Program Files\\LLVM\\x86_64-w64-mingw32\\include\\c++\\\\x86_64-w64-mingw32” -internal-isystem “C:\\Program Files\\LLVM\\x86_64-w64-mingw32\\include\\c++\\\\backward” -internal-isystem “C:\\Program Files\\LLVM\\include\\c++\\” -internal-isystem “C:\\Program Files\\LLVM\\include\\c++\\\\x86_64-w64-mingw32” -internal-isystem “C:\\Program Files\\LLVM\\include\\c++\\\\backward” -internal-isystem “include\\c++” -internal-isystem “include\\c++\\x86_64-w64-mingw32” -internal-isystem “include\\c++\\backward” -internal-isystem “C:\\Program Files\\LLVM\\lib\\clang\\7.0.0\\include” -internal-isystem “C:\\Program Files\\LLVM\\x86_64-w64-mingw32/sys-root/mingw/include” -internal-isystem “C:\\Program Files\\LLVM\\x86_64-w64-mingw32\\include” -internal-isystem “C:\\Program Files\\LLVM\\include” -fdeprecated-macro -fdebug-compilation-dir “C:\\Users\\krzys” -ferror-limit 19 -fmessage-length 0 -fno-use-cxa-atexit -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fseh-exceptions -fdiagnostics-show-option -o “C:\\Users\\krzys\\AppData\\Local\\Temp\\clang-test-4710bf.o” -x c++ clang-test.cpp
clang -cc1 version 7.0.0 based upon LLVM 7.0.0 default target x86_64-pc-win32
ignoring nonexistent directory “C:\Program Files\LLVM\x86_64-w64-mingw32\include\c++”
ignoring nonexistent directory “C:\Program Files\LLVM\x86_64-w64-mingw32\include\c++\x86_64-w64-mingw32”
ignoring nonexistent directory “C:\Program Files\LLVM\x86_64-w64-mingw32\include\c++\backward”
ignoring nonexistent directory “C:\Program Files\LLVM\x86_64-w64-mingw32\include\c++\”
ignoring nonexistent directory “C:\Program Files\LLVM\x86_64-w64-mingw32\include\c++\\x86_64-w64-mingw32”
ignoring nonexistent directory “C:\Program Files\LLVM\x86_64-w64-mingw32\include\c++\\backward”
ignoring nonexistent directory “C:\Program Files\LLVM\include\c++\”
ignoring nonexistent directory “C:\Program Files\LLVM\include\c++\\x86_64-w64-mingw32”
ignoring nonexistent directory “C:\Program Files\LLVM\include\c++\\backward”
ignoring nonexistent directory “include\c++”
ignoring nonexistent directory “include\c++\x86_64-w64-mingw32”
ignoring nonexistent directory “include\c++\backward”
ignoring nonexistent directory “C:\Program Files\LLVM\x86_64-w64-mingw32/sys-root/mingw/include”
ignoring nonexistent directory “C:\Program Files\LLVM\x86_64-w64-mingw32\include”
#include “…” search starts here:
#include <…> search starts here:
C:\Program Files\LLVM\lib\clang\7.0.0\include
C:\Program Files\LLVM\include
End of search list.
clang-test.cpp:1:10: fatal error: ‘iostream’ file not found
#include
^~~~~~~~~~
1 error generated.

MinGW-w64

We will download MinGW-w64 which contains headers we need for clang. Go to project’s page on SourceForge, choose latest stable build with POSIX threads and SEH exception model. In this article, we use x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z. Unpack archive (I put mingw64 in C:\Program Files\). You will find a number of standard tools in bin folder. Add this folder to PATH, so that clang can find it.

MinGW in Path

Tip: make is bundled as mingw32-make.exe. You can run mklink "C:\Program Files\mingw64\bin\make.exe" "C:\Program Files\mingw64\bin\mingw32-make.exe" in Windows’ CMD as Administrator to create a soft link from make.

Building with clang++

MSVC is the default toolchain for Windows, so you will have to set target manually. Everything works now!

Clang++: Working setup

Verbose output:

kantoniak@kantoniak-razer:~ $ clang++ –verbose -target x86_64-pc-windows-gnu clang-test.cpp
clang version 7.0.0 (tags/RELEASE_700/final)
Target: x86_64-pc-windows-gnu
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
“C:\\Program Files\\LLVM\\bin\\clang++.exe” -cc1 -triple x86_64-pc-windows-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name clang-test.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -v -resource-dir “C:\\Program Files\\LLVM\\lib\\clang\\7.0.0” -internal-isystem “C:\\Program Files\\mingw64\\x86_64-w64-mingw32\\include\\c++” -internal-isystem “C:\\Program Files\\mingw64\\x86_64-w64-mingw32\\include\\c++\\x86_64-w64-mingw32” -internal-isystem “C:\\Program Files\\mingw64\\x86_64-w64-mingw32\\include\\c++\\backward” -internal-isystem “C:\\Program Files\\mingw64\\x86_64-w64-mingw32\\include\\c++\\8.1.0” -internal-isystem “C:\\Program Files\\mingw64\\x86_64-w64-mingw32\\include\\c++\\8.1.0\\x86_64-w64-mingw32” -internal-isystem “C:\\Program Files\\mingw64\\x86_64-w64-mingw32\\include\\c++\\8.1.0\\backward” -internal-isystem “C:\\Program Files\\mingw64\\include\\c++\\8.1.0” -internal-isystem “C:\\Program Files\\mingw64\\include\\c++\\8.1.0\\x86_64-w64-mingw32” -internal-isystem “C:\\Program Files\\mingw64\\include\\c++\\8.1.0\\backward” -internal-isystem “C:\\Program Files\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include\\c++” -internal-isystem “C:\\Program Files\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include\\c++\\x86_64-w64-mingw32” -internal-isystem “C:\\Program Files\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include\\c++\\backward” -internal-isystem “C:\\Program Files\\LLVM\\lib\\clang\\7.0.0\\include” -internal-isystem “C:\\Program Files\\mingw64\\x86_64-w64-mingw32/sys-root/mingw/include” -internal-isystem “C:\\Program Files\\mingw64\\x86_64-w64-mingw32\\include” -internal-isystem “C:\\Program Files\\mingw64\\include” -fdeprecated-macro -fdebug-compilation-dir “C:\\Users\\krzys” -ferror-limit 19 -fmessage-length 0 -fno-use-cxa-atexit -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fseh-exceptions -fdiagnostics-show-option -o “C:\\Users\\krzys\\AppData\\Local\\Temp\\clang-test-f81641.o” -x c++ clang-test.cpp
clang -cc1 version 7.0.0 based upon LLVM 7.0.0 default target x86_64-pc-win32
ignoring nonexistent directory “C:\Program Files\mingw64\x86_64-w64-mingw32\include\c++”
ignoring nonexistent directory “C:\Program Files\mingw64\x86_64-w64-mingw32\include\c++\x86_64-w64-mingw32”
ignoring nonexistent directory “C:\Program Files\mingw64\x86_64-w64-mingw32\include\c++\backward”
ignoring nonexistent directory “C:\Program Files\mingw64\x86_64-w64-mingw32\include\c++\8.1.0”
ignoring nonexistent directory “C:\Program Files\mingw64\x86_64-w64-mingw32\include\c++\8.1.0\x86_64-w64-mingw32”
ignoring nonexistent directory “C:\Program Files\mingw64\x86_64-w64-mingw32\include\c++\8.1.0\backward”
ignoring nonexistent directory “C:\Program Files\mingw64\include\c++\8.1.0”
ignoring nonexistent directory “C:\Program Files\mingw64\include\c++\8.1.0\x86_64-w64-mingw32”
ignoring nonexistent directory “C:\Program Files\mingw64\include\c++\8.1.0\backward”
ignoring nonexistent directory “C:\Program Files\mingw64\x86_64-w64-mingw32/sys-root/mingw/include”
#include “…” search starts here:
#include <…> search starts here:
C:\Program Files\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0\include\c++
C:\Program Files\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0\include\c++\x86_64-w64-mingw32
C:\Program Files\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0\include\c++\backward
C:\Program Files\LLVM\lib\clang\7.0.0\include
C:\Program Files\mingw64\x86_64-w64-mingw32\include
C:\Program Files\mingw64\include
End of search list.
“C:\\Program Files\\mingw64\\bin\\ld.exe” -m i386pep -Bdynamic -o a.exe “C:\\Program Files\\mingw64\\x86_64-w64-mingw32\\lib\\crt2.o” “C:\\Program Files\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\crtbegin.o” “-LC:\\Program Files\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0” “-LC:\\Program Files\\mingw64\\x86_64-w64-mingw32\\lib” “-LC:\\Program Files\\mingw64\\lib” “-LC:\\Program Files\\mingw64\\x86_64-w64-mingw32/sys-root/mingw/lib” “C:\\Users\\krzys\\AppData\\Local\\Temp\\clang-test-f81641.o” -lstdc++ -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt “C:\\Program Files\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\crtend.o”

Have fun!

You can build Konstruisto now 😉