The following steps apply only to MSYS2 64-bit installation of QT Creator 4.5.1 (install instructions here), where you also have MinGW-w64 and mingw32-make installed on MSYS2; and you are building a non-QT C or C++ application.
These instructions use QMake, because QBS doesn't support MSYS2 clang. Well, QMake doesn't support it either, but I did figure out how to add support to QMake and I didn't figure out QBS.
There is QMake support for MSVC-clang but it outputs MSVC makefiles, so you can't build it with MSYS2 make. So that does not apply to us.
- Install clang with
pacman
. I used pacman -Ss mingw-w64-x86_64-clang
, your flavour may vary.
- Add support for clang to QMake:
- In the MSYS2 shell, go into
/msys64/mingw64/share/qt5/mkspecs/
- Do
cp -a win32-g++ win32-clang-msys
- Edit
win32-clang-msys/qmake.conf
and change gcc
to clang
, and g++
to clang++
(2 places each)
- In the same file, take out
-fno-keep-inline-dllexport -mthreads
which are not supported by clang.
- In QT Creator, set up a new Kit:
- Go to Manage Kits.
- Add a Custom Compiler for C and browse to the installed path (
/mingw64/bin/clang.exe
under your MSYS2 install).
- Add a Custom Compiler for C++ as
clang++.exe
in the same place)
- Add a manual Kit called
Clang
and set those two compilers as its compilers.
- In the manual kit config set "Qt mkspec" as
win32-clang-msys
- Set "QT Version" to something. Even though I am using a non-QT project, the IDE doesn't like using the kit if "Qt version" is set to None.
Now you can attempt to build your project with the Clang
kit and QMake.
I initially tried with QBS and the build failed due to this bug . But the build commands do succeed if I copy-paste them and cut out the bogus -target
switch. So for QBS users I guess you have to switch to QMake in the meantime until they fix that bug.
Troubleshooting: I sometimes got an error Project ERROR: failed to parse default search paths from compiler output
. This is a problem with QMake's lack of support for clang. The error tended to not occur if I built in a subdirectory of the .pro
file, but did occur if I built in a sibling directory.
As a workaround: go back into win32-clang-msys/qmake.conf
. Change the first clang
back to g++
. Then "Run Qmake" (from QT creator or commandline), then change it back. The first time you run QMake it writes the file .qmake.stash
and then does not need to generate it again. The contents of this file were bogus for me but building seemed to work anyway.
Undefined references: I found that linking with -static
produced a bunch of undefined references to __imp__cxa_
names. Not sure what the problem is here but maybe related to the bug with generating .qmake.stash
. I guess the Qt developers would need to officially add non-MSVC Clang support to QMake.
Multiple definitions: The CLang linker gave multiple definitions for inline DLLexport functions. I found no workaround yet for this; g++ has -fno-keep-inline-dllexport
to avoid this problem but CLang 5 does not support that flag.