Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging in QtCreator using MSVC2017 compiler

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?

like image 579
Matthew Kraus Avatar asked Dec 12 '17 12:12

Matthew Kraus


People also ask

Is Qt Creator a 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.

What compiler does Qt Creator use?

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.


1 Answers

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:

Reference System

  • Windows 7 SP1 x64 (works exactly the same with Win10, also)
  • Visual Studio 2017 (version 15.5.1)
  • Qt 5.10.0
  • Qt Creator 4.5.0 (MSVC 2015, 64-bit)
  • Debugging Tools for Windows (CDB.exe), which does NOT ship with Visual Studio 2017
    • via Windows 10 SDK
  • Targeting Windows Desktop 64-bit applications

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.

Prerequisites

  • Windows 7 x64, or Windows 10
  • Visual Studio 2017 (tested with version 15.5.1)

Assuming you have Win7/Win10 and MSVC2017 installed, let's get started!

Download Qt

  • Download the file qt-opensource-windows-x86-5.10.0.exe from the Qt Official Release Archive
  • Install with the option "msvc2017 64-bit" checked.
    • all of the other check boxes are not necessary for the configuration described here

Qt 5.10 Setup

Install the Windows SDK for Windows 10 - https://developer.microsoft.com/en-us/windows/downloads/sdk-archive

Windows 10 SDK

  • select "Debugging Tools for Windows" and uncheck everything else

Windows 10 SDK feature selection

  • The web installer will download the selected component and create a folder called "Windows Kits."
  • Install these two files (the x86 version is optional):

    • Windows Kits\10\WindowsSDK\Installers\X64 Debuggers And Tools-x64_en-us.msi
    • Windows Kits\10\WindowsSDK\Installers\X86 Debuggers And Tools-x86_en-us.msi

Win SDK debugger installers

Install the 64-bit STAND-ALONE installer for QtCreator

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.

  • Download "Qt Creator 4.5.0 for Windows 64-bit"

enter image description here

  • this version seems to have the option to select "CDB Debugger Support" (not found when installing the full Qt package that has Qt Creator bundled in)

Qt Creator 4.5.0 (64-bit) setup

Configure Kits in QtCreator

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.

QtCreator kit configuration order

Configure Debuggers in QtCreator

  • Set the path to C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\cdb.exe and give it a name like "Window Kit 10 cdb x64".

Debugger Tab in QtCreator

  • 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"?.

cpp-64-bit-compiler

Now configure the Kit

  1. Click Add.
  2. Name - Give the name of the Kit; this will be the configuration you use when compiling your application.
  3. Compiler - Select the "Microsoft Visual C++ Compiler 15.0 (amd64)" for C and C++.
  4. Debugger - Select "Windows Kit 10 cdb x64" that we configured on the "Debuggers" tab
  5. Qt version - Select "Qt 5.10.0 (msvc2017_64). It is important to compile with the same bit-ness as the Qt libraries that you will link against.
  6. Click Apply.

Kit Setup in Qt Creator

Verify debugging works

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:

Debugging 64-bit code in Qt Creator

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();
}
like image 129
Matthew Kraus Avatar answered Sep 29 '22 07:09

Matthew Kraus