简体   繁体   中英

How to use Clang kit with Qt Creator on Windows?

(this question similar to this one but is not the same)

I'm using QtCreator on Windows platform, usually with vc toolchain. sometimes with MinGW-W64, so for now, I'd like to try clang for some reasons. unlike the post above - I don't use Qt library , just qtcreator as IDE, so I suppose I don't need to re-build it and QtCreator for a using clang kit, is it correct?

there are a few questions about that:

  1. CLang distribution doesn't contain platform or even c/c++ runtime library, should i use it from vc kit? or/and MinGW runtime? how to switch between?
  2. Correct my understanding please if it's wrong - for Win platform, clang has two options to use: 1) normal use - clang.exe ,as on any other platform 2) clang-cl.exe - additional layer which "looks like" cl.exe, and just parses cl command line keys and calls normal clang.
  3. as I understand there is no LLDB for Windows platform , can I use GDB or CDB depends of the used runtime lib and binary format of the executable?

and finally - how to configure all this in qtcreator?

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.

  1. Install clang with pacman . I used pacman -Ss mingw-w64-x86_64-clang , your flavour may vary.
  2. Add support for clang to QMake:
    1. In the MSYS2 shell, go into /msys64/mingw64/share/qt5/mkspecs/
    2. Do cp -a win32-g++ win32-clang-msys
    3. Edit win32-clang-msys/qmake.conf and change gcc to clang , and g++ to clang++ (2 places each)
    4. In the same file, take out -fno-keep-inline-dllexport -mthreads which are not supported by clang.
  3. In QT Creator, set up a new Kit:
    1. Go to Manage Kits.
    2. Add a Custom Compiler for C and browse to the installed path ( /mingw64/bin/clang.exe under your MSYS2 install).
    3. Add a Custom Compiler for C++ as clang++.exe in the same place)
    4. Add a manual Kit called Clang and set those two compilers as its compilers.
    5. In the manual kit config set "Qt mkspec" as win32-clang-msys
    6. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM