How to use Clang kit with Qt Creator on Windows? - c++

(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:
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?
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.
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.
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.

Related

How can i use neovim and coc.nvim for develop windows c++ apps on linux

I develop c++ apps on linux and i use neovim with coc.nvim and coc-clangd plugins.
I want to develop an app for windows but i comfort with linux and neovim so i want to use them for it. But i get some include errors with some windows headers (etc. "windows.h").
I use linux only for writing the code and i'll compile the program on windows. How can i prevent this errors and use windows headers with coc.nvim?
i'll compile the program on windows
You can cross-compile it from Linux. It's only marginally more difficult than getting the code completion to work.
Get the standard library headers (and libraries, if you want to cross-compile) from MinGW.
Your package manager might have those, or you can get them from https://winlibs.com/.
I prefer getting those from MSYS2, and made scripts to automate this (since MSYS2 is otherwise Windows-only):
git clone https://github.com/holyblackcat/quasi-msys2
cd quasi-msys2/
make install _gcc
Figure out the Clang flags needed to cross-compile.
Unlike GCC, which for every target platform requires a separate compiler distribution, Clang is inherently a cross-compiler. You only need a single Clang distribution to compile for any supported platform.
Download Clang from your package manager, and point it to the freshly downloaded headers and libraries.
Following flags work for me: clang++-14 1.cpp --target=x86_64-w64-mingw32 --sysroot=/path/to/quasi-msys2/root/mingw64 -fuse-ld=lld-14 -pthread -stdlib=libstdc++ -femulated-tls -rtlib=libgcc.
--target and --sysroot are crucial. The latter needs to point to the files you've downloaded. The remaining flags are less important.
Running this should produce a.exe, runnable with wine a.exe.
Feed the same flags to Clangd.
There are several ways to set compiler flags for Clangd.
The easiest one is to create a file named compile_flags.txt in your project directory, and put the flags into it, one per line:
--target=x86_64-w64-mingw32
--sysroot=/path/to/quasi-msys2/root/mingw64
-fuse-ld=lld-14
-pthread
-stdlib=libstdc++
-femulated-tls
-rtlib=libgcc
Then Clangd should do the right thing for any source files in this directory.
Apparently, my Quasi-MSYS2 can somewhat automate this.
After running the commands above (make install _gcc and others), run make env/shell.sh, and run your editor from this shell.
Replace compiler_flags.txt with compiler_commands.json with following contents:
[
{
"directory": "/your/sources",
"file": "/your/sources/1.cpp",
"command": "win-clang++ 1.cpp"
}
]
Where win-clang++ is a Clang wrapper I ship, which automatically adds the flags I listed above.
Configure your editor to add following flag to Clangd: --query-driver=/path/to/win-clang++ (use which win-clang++ from quasi-msys2 shell to get the full path).
This makes Clangd obtain the right flags automatically from this wrapper.
You can't use windows.h while you're compiling a Linux native application. If want to make your application platform ready and you're using some kind of OS native cals, then you have to probably use defines like #if _WIN32/__linux__ and so on. At the end, you can cross-compile your application to Windows while you're running on Linux as well.

Make - Internal compiler Error under QT 5.14.2 "Q_CORE_EXPORT"

I just installed QT Creator with QT under Win10 to build an already existing project. (Under Ubuntu everything went fine running the Make file). I'm not an expert for QT therefore I'm not able to find out how to resolve the error:
C:\Qt\5.14.2\mingw73_64\include/QtCore/qfloat16.h:102:54: internal compiler error: in make_rtl_for_nonlocal_decl, at cp/decl.c:6590
Q_CORE_EXPORT static const quint32 mantissatable[];
My gcc version is 8.3.0 (x86_64-posix-seh, Built by strawberryperl.com project). Is there something missing or broken in the installation?
On windows, you generally need to have a Qt which was built with the same (or compatible, but that can be hard to verify) compiler and relevant build options, as what you are using to build your application.
I doubt you will find a pre-built Qt SDK for that version of gcc, so if you want to use it, you should build Qt from sources. It can be a bit tedious on Windows, there are a fewf prerequisites you have to get etc. I recommend you use the Qt online installer to install a MinGW version of Qt SDK, and matching version of MinGW (also offered by the Qt installer.
I just found out from qmake.stash, that the included script for creating the make file always referenced a false path for the gcc compiler. I therefore build i manually with the QT Creator and it worked as expected. So I guess the fault was due to different paths for gcc in the environmental variables.
Here is the bug, there is a link to the patch: https://github.com/msys2/MINGW-packages/issues/5006
Also you can just downgrade to mingw gcc 8.2.0

What should I change in Qt creator to compile using MinGW rather than microsoft compiler

Currently my qmake command in QtCreator looks like this:
qmake.exe D:\programing\myproject\myproject.pro -r -spec win32-msvc2010 "CONFIG+=debug"
Now I do not like the win32-msvc2010 because Microsoft compiler sucks horribly doesn't support C++0x.
So what configuration do I change to use MinGW instead of Microsoft compiler when compiling desktop Windows application?
I walked through the settings and googled but no hints...
Easiest thing to do would be to go here:
Link to Qt downloads page
Scroll down and select Qt 5.6.0 for Windows 32-bit (MinGW 4.9.2, 1.0 GB) this will download the installer (its about 1 GB). Run it and choose all the default options. Then you will have Qt 5.6 with mingw and Qt Creator all setup ready to go.
Note: Before you start that its probably best to delete your current version - if you are not attached to it in any way :)
If you want to change your current setup, then it might be longer-winded to find out what you are missing for mingw. For example you need the mingw qmake (and all the other stuff under the mingw_32 folder) file that would be located here (using default install options):
C:\Qt\Qt5.5.1\5.5\mingw492_32\bin\qmake.exe
to create your "Qt Version" part of the kit. And the actual mingw compiler, which is located here (using default install options):
C:\Qt\Qt5.5.1\Tools\mingw492_32\bin\g++.exe
to create your compiler. Once you have both of these then you can put them together to create your new kit.
But to just add a compiler all you need to do is:
Click compilers tab
Click add and select mingw
A new compiler is added, click it
You will see some options below, add your compiler executable path in (like the one above for example).
And you are done - there are some other options, but you probably don't need to use them.
note my paths are for qt 5.5.1 (obviously) so slightly different to 5.6 :)

How install 64-bit Qt on Windows with C++11 support?

IN A NUTSHELL:
My 64-bit Qt build doesn't have C++11 support.
My Qt build with C++11 is 32-bit.
Qt5 using the online installer is 32-bit
Despite having dedicated the last 3 days of my life to this, I can't get 64-bit Qt5 to build from source.
Help! Something must be incompatible in my environment, but what could it be?
So, I need to develop 64-bit Qt apps with C++11 on Windows. I've found threads/guides here, here, here, here and here, and none of them work or apply, though I note the second of these let me build Qt 4.8.3 with C++11 support (but 32-bit). The third one looks more complicated than it should be and also involves building with VS 2010, which seems like part of the problem, so I haven't tried it. I have also wasted a lot of time trying to build previous versions with different compilers, like mingw and clang, so I'm pretty sure that cl is what I need to use.
Basically, I've gotten to the point where I can build Qt4.8.3 and execute 64-bit apps from within QtCreator 2.6.1, but I can't get full C++11 support. It finds only cl from VS 10.0 which has only very limited C++11 support. Figuring out how to get it to use cl from VS 11.0 would be one solution to my problem. But when I try
QMAKE_CXX = C:/Program Files (x86)/Microsoft Visual Studio
11.0/VC/bin/cl
the build fails with a jom Error 2, and if I select the option for building with nmake I get a similar error.
Qt 5 should have full C++11 support. But when I try to build it myself from source, it fails. The reason for this seems to be that despite running configure/make from the VS2012 x64 Cross Tools command prompt, specifying -platform win32-2012 in the configure step, and specifying QMAKE_COMPILER_DEFINES += _MSC_VER=1700 WIN32 as per
this guide, in both the win32-msvc2010 and win32-msvc2012 mkspecs, it still builds with the cl from VS 2010 and that is causing some kind of conflict. Here is my configure command:
configure -static -debug-and-release -confirm-license -opensource
-nomake tests -nomake exampmles -opengl desktop -no-libjpeg -platform win32-msvc2012
And here's the linker failure when I try to build:
At the end of the top 2 paragraphs you can see that msvc-2010 is being included, and earlier in the console output (not shown) there is -D_MSC_VER=1600 in the moc commands.
So I'm at a loss, can anyone help?
Edit: I had stated that I was building 64-bit apps with Qt5, actually I'm able to build 64-bit apps (w/o C++11 support) off a build of Qt 4.8.3. If I try this with Qt5, I get this error:
LNK1112: module machine type 'X86' conflicts with target machine type
'x64'
So this leads back to compiling Qt5 in 64-bit mode, which I can't seem to do.
Finally got a 'good enough' solution by going back and rebuilding 4.8.3 in 64-bit mode, this time ensuring that it was built with C++11 support. Since a solution involving Qt5 is what would be ideal, I'll leave this open for awhile to see if anyone can figure out how to do that.
Importantly, I note that I successfully built 64-bit, C++11-supporting 4.8.3 from the same prompt I had been using to try to build 5.0. Just changed directories. So same environment, everything. And I also have downloaded all the extra prereqs required for Qt5.
You can find prebuilds of Qt 5.0.1 SDK with 64bit support here:
http://releases.qt-project.org/digia/5.0.1/latest/
Tested and working :)
I found Qt 4.8.5 windows version qt-win-opensource-4.8.5-vs2008.exe at https://download.qt.io/archive/qt/4.8/4.8.5/ for VC++ projects.

Qt 4.8.2 With GCC 4.7.0.1 Keeps Crashing

I've downloaded Qt 4.8.2 library, Qt Creator 2.5.2, and manually installed MingW with w32api version 3.13 and GCC/g++ version 4.7.0.1. My OS is Windows 7 Ultimate x64.
I can create a sample "Plain C++ project" in Qt Creator; compile and run that console application using g++ without any issue.
But I cannot run a Qt application. I used Qt Creator, created a dummy Qt Application using Creator's "Qt Gui Application" template. The project can be compiled successfully, without any error or warning. But the binary keeps crashing when I try to run (both from Qt Creator and Windows Explorer) it. Both debug and release builds crash. It crashes even before showing main window.
MingW is installed in C:\MingW and C:\MingW\bin is in PATH. Qt is installed in C:\Qt\4.8.2 and C:\Qt\4.8.2\bin is in PATH.
I analyzed generated exe of the Qt Gui Application output with Dependency Walker and found that it found all required DLLs:
c:\windows\system32\KERNEL32.DLL
c:\windows\system32\MSVCRT.DLL
c:\mingw\bin\LIBGCC_S_DW2-1.DLL
c:\mingw\bin\LIBSTDC++-6.DLL
c:\qt\4.8.2\bin\QTCORE4.DLL
c:\qt\4.8.2\bin\QTGUI4.DLL
So, what's causing the runtime crash?
EDIT
I also tried Qt's example projects: 2dpainting and addressbook - both crashed when they were launched.
You should build Qt with the MinGW compiler you're using to build your application. GCC is generally less sensitive to binary compatibility issues than MSVC is, but Qt is a big, complex framework library. If anything would expose those kinds of issues, Qt would probably be on the short list.
Building Qt is pretty straightforward, but it takes a lot of time and there always seems to be two or three patches I need to make to get things to build successfully.
The last time I built Qt (4.7.3) with MinGW, I had to make the following patches - I'm not sure whether they will still apply to Qt 4.8:
make sure not to enable C++11 mode in the compiler - there are several macros with concatenated string literals that break under the new C++11 extended literal syntax
there is a problem with how some distributions of MinGW incorporate the Microsoft extensions to float.h - I had to sometimes had to add the line:
#include_next <float.h>
to the end of the MinGW-specific float.h so the generic GCC float.h would get processed properly. I had to do this for nuwen 4.7.0 lib/gcc/i686-pc-mingw32/4.7.0/include/float.h and TDM 4.6.1 32-bit distro lib/gcc/mingw32/4.6.1/include/float.h (the 64-bit distro of TDM didn't need this patch).
patch qmake\Makefile.win32-g++ and qmake\Makefile.win32-g++-sh to remove the -static-libstdc++ option that GCC doesn't recognize (and now errors out on instead of ignores)
patch mkspecs/win32-g++/qmake.conf to move the -Wl, in the QMAKE_LFLAGS_EXCEPTIONS_ON macro to its proper place in QMAKE_FLAGS:
QMAKE_LFLAGS = -Wl,-enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc
QMAKE_LFLAGS_EXCEPTIONS_ON = -mthreads
copy make.exe to mingw32-make.exe in MinGW's bin directory if there's not already a mingw32-make.exe
Then building Qt consists of:
set QTDIR=<location of Qt source directory> # where configure.exe is
set PATH=%QTDIR%\bin;c:\MinGW\bin;%PATH%
set INCLUDE=
set LIB=
cd %QTDIR%
mingw32-make confclean # (this should fail the first time, since there's nothing to clean)
configure.exe -opensource -debug-and-release -nomake examples -nomake demos -nomake tests -platform win32-g++ # and accept the GPL license
mingw32-make
This takes a while... hopefully nothing else will need patching.
I also got this problem. I'm a Qt n00b and tought, when installing Qt-libraries, that "well I already have Mingw installed so I skip installing the Mingw that comes with Qt". That gave me prolems. When installing mingw that came with Qt everything worked ok.
So my advice to anyone googling to this question (like I did) is to instead of using your already installed Mingw, install the one with Qt and use that (otherwise you have to build the Qt libraries within your Mingw, like the answer from Michael Burr)