I've installed Qt, but I'm getting errors when trying to debug C++ code from within QtCreator. I'm using Visual Studio 2017 on Windows, and it seems like the debugger that QtCreator needs (cdb.exe) is not installed. How can I configure debugging to work for 64-bit code within QtCreator when using the MSVC2017 compiler?
Qt is supported on a variety of 32-bit and 64-bit platforms, and can usually be built on each platform with GCC, a vendor-supplied compiler, or a third party compiler. In Qt Creator, a kit specifies the compiler and other necessary tools for building an application for and running it on a particular platform.
Qt Creator uses the C++ compiler from the GNU Compiler Collection on Linux. On Windows it can use MinGW or MSVC with the default install and can also use Microsoft Console Debugger when compiled from source code. Clang is also supported.
What I discuss here is getting QtCreator working with the Visual Studio 2017 (MSVC2017) compiler--including debugging! A default installation of MSVC2017 and Qt 5.10 will allow you to compile and run code, but you won't get debugging support by default. I will show the steps necessary to get everything installed (including debugging support) for the following reference system:
I've tested these steps on both Windows 10 and Windows 7 (x64) and it seems to be working. Note that the version of QtCreator that comes bundled with the Qt installer is the 32 bit version, and it won't work out of the box if you're trying to do debugging using MSVC. This is documented, but not step-by-step (see QtCreator Windows Debugging and QtCreator Debugger Engines), and not everything is in one place, so I'm going to provide simple instructions here on how to get things working for the reference system I describe above.
Assuming you have Win7/Win10 and MSVC2017 installed, let's get started!
Install the Windows SDK for Windows 10 - https://developer.microsoft.com/en-us/windows/downloads/sdk-archive
Install these two files (the x86 version is optional):
NOTE: if you install QtCreator AFTER installing the Windows debugging tools, QtCreator will automatically detect the location of the debuggers, saving you the hassle when creating the kits. If you have already installed QtCreator, don't worry, configuring the kits in QtCreator is shown below.
In QtCreator parlance or terminology, a "Kit" is a combination of debugger + compiler + Qt version + others. (In this sense, the word "Kit" does not mean the same thing as it does for "Windows Kits" when we installed the debugger above; this is an unfortunate name collision.) Set up a "QtCreator Kit" by selecting Tools -> Options -> Build & Run. I like to start on the far right tab and work to the left, ending on "Kits." The reason for this is that all of the tabs (1,2,3) below need to be configured in order to make a complete Kit.
C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\cdb.exe
and give it a name like "Window Kit 10 cdb x64".Set up the compiler. Make sure "Microsoft Visual C++ Compiler 15.0 (amd64)" is in the list. Note that QtCreator will auto-detected these locations if you've installed things to the default paths. Add the path to the compiler if it isn't listed.
Note that for Visual Studio 2017 x64 bit, you want the "amd64" version, which is the 64-bit compiler. In case you are wondering why there are so many compilers listed--they are "cross-compilers." That means they target different architectures, so for example, the "x86_amd64" is a 32-bit process that creates a 64-bit application; you would use this version if you were developing 64-bit code on a 32-bit computer (hence the term cross-compiling). Since this tutorial assumes you're running on a 64-bit OS, use the amd64 version. (The "x86_amd64" cross-compiler will also generate 64-bit code.) When I first saw the huge list of compiler selections in QtCreator, I didn't know which one to pick. Now you know, too! See When compiling x64 code, what's the difference between "x86_amd64" and "amd64"?.
Let's have some fun and write a 64-bit program, and then debug it. A good test is to allocate lots of memory (typical 32-bit Windows processes are limited to 2GB, see How much memory can a 32 bit process access on a 64 bit operating system? ). Allocating, say, 32GB of RAM should be a positive indicator that we are indeed compiling a 64-bit application. Here is a screen shot of QtCreator debugging a 64-bit Windows test program:
Note that I'm using the MSVC2017 64-bit compiler and the CDB.exe debugger mentioned above.
#include <QCoreApplication>
#include <cassert>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int intSize = sizeof(int);
assert(intSize == 4);
int intPtrSize = sizeof(int*);
assert(intPtrSize == 8);
constexpr size_t giga = 1000 * 1000 * 1000;
size_t _32gigs = 32 * giga / intSize;
int* gigaChunk = new int[_32gigs];
assert(gigaChunk != nullptr);
return a.exec();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With