Building a simple Qt application on Windows with Visual Studio

After preparing the Qt framework, I needed to check if everything works; therefore I built a very simple Qt (5.2.1) application, the GUI-equivalent of a “Hello-World” program.

Building the sample application

  1. Open “Visual Studio 2010” and create a new, blank project:
    File -> New -> Project…
    Visual C++ -> General -> Empty Project

  2. Add a source code file to the project with the following content:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QLabel>
        
    int main (int argc, char* argv[])
    {
        QApplication app(argc, argv);
        QLabel* label = new QLabel("Hello, world!");
        label->show();
        return app.exec();
    }
    
  3. Open Build -> Configuration Manager and under Active Solution Configuration select New…
    Name it “Release (x64)” respectively “Debug (x64)” (copy settings from: Release respectively Debug).

  4. Switch Active Solution Configuration to Release (x64) (respectively Debug (x64)) and switch the platform for both to x64.

  5. Open Project -> Properties and set the configuration parameters to the following values (this assumes a release build):

    Configuration properties: General

    Character Set: Use Unicode Character Set
    C/C++: General
    Additional Include Directories: C:\devel\ext\Qt\5.2.1\_64DLL\include
    Linker: General
    Additional Library Directories: C:\devel\ext\Qt\5.2.1\_64DLL\lib
    Linker: Input
    Additional Dependencies: Qt5Core.lib Qt5Gui.lib Qt5Widgets.lib (the import libraries for the required Qt DLLs)

Running the sample application

After this, the build process was successful and the result was a small executable file (13 KB).
But the launch of the application failed, with an error message stating that Qt5Core.dll was missing.

The reason: I am using the shared-files approach, meaning the Qt runtime libraries (DLLs) must be present to the program; if built as a static program, they would have been incorporated/linked into the executable file directly.

The solution: Several of the previously built Qt runtime libraries needed to be copied from C:\devel\ext\Qt\5.2.1\_64DLL\lib into the working directory of the *.exe:

That way, the small program grew from a mere 13 Kilobyte utility to a 20 Megabyte monster (in the case of static linkage, the same would have happened, only that it would have produced one big exe-file).

But at least the GUI app was running — …but with an console window hanging in the background while the Qt application was open…
To get rid of that, I changed these options in Project -> Properties:

Linker: System

SubSystem: Windows (/SUBSYSTEM:WINDOWS)

On the other hand, an additional console window [/SUBSYSTEM:CONSOLE] can come handy at times, for example if one wants to use qDebug() or std::cout for some diagnostic output.)

Linker: Advanced

Entry Point: mainCRTStartup

So far, so good.