c++windowsqtqtcorecl.exe

How to compile a Qt program without qtCreator on Windows?


I have read the question Can I use Qt without qmake or Qt Creator? which is basically the same for Linux, and very useful.

How to compile a basic program using QtCore (console application, even without GUI) on Windows, without using qmake or qtCreator IDE, but just the Microsoft VC++ compiler cl.exe?

For example, let's say we have:

#include <iostream>
#include <QtCore>
int main()
{
    QVector<int> a; // Qt object
    for (int i=0; i<10; i++)
        a.append(i);
    std::cout << "hello";
    return 0;
}

Using:

call "C:\path\to\vcvarsall.bat" x64
cl main.cpp /I D:\coding\qt\qtbase-everywhere-src-5.15.5\include

fails with:

D:\coding\qt\qtbase-everywhere-src-5.15.5\include\QtCore\QtCore(3): fatal error C1083: Cannot open include file: 'QtCore/QtCoreDepends': No such file or directory

Indeed this file is not present in the release qtbase-everywhere-opensource-src-5.15.5.zip from https://download.qt.io/archive/qt/5.15/5.15.4/submodules/.

TL;DR: More generally, which cl.exe arguments should we use to to able to use all Qt includes, and effectively compile such a minimal project using QtCore?


Solution

  • I finally managed to do it 100% from command line, without the qtCreator IDE, but not yet without qmake. Steps to reproduce:


    Addendum: here is solution now for a minimal GUI app:

    main.cpp

    #include <QtCore/QCoreApplication>
    #include <QTextStream>
    #include <QMessageBox>
    #include <QApplication>
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QMessageBox::information(NULL, "Hello", "Hello", "Ok");
        return a.exec();
    }
    

    qt_example_gui.pro

    TEMPLATE = app
    TARGET = qt_example_gui
    INCLUDEPATH += .
    SOURCES += main.cpp
    QT += gui widgets
    

    Do the vcvarsall.bat x64, qmake, nmake like in the solution above. No be sure you have this file structure:

    release\qt_example_gui.exe
    release\Qt5Core.dll
    release\Qt5Gui.dll
    release\Qt5Widgets.dll
    release\platforms\qwindows.dll
    

    Run the .exe, that's it!