Home > Net >  How to compile a Qt program without qtCreator on Windows?
How to compile a Qt program without qtCreator on Windows?

Time:07-09

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?

CodePudding user response:

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

  • Let's assume Microsoft MSVC 2019 is installed.

  • Install qt-opensource-windows-x86-5.14.2.exe. (This is the latest Windows offline installer I could find), double check that you install at least msvc2017_64.

    Note: Don't use qtbase-everywhere-opensource-src-5.15.4.zip: using the include subfolder from this package for cl.exe /I ... is not enough. (I thought it would, at first)

  • Create a folder example containing the main.cpp file above

  • Open a command line window in this folder and do:

    vcvarsall.bat x64
    
  • Now either do "c:\path\to\msvc2017_64\bin\qmake.exe" -project to create a example.pro project file or create it manually with:

    TEMPLATE = app
    TARGET = qt_example
    INCLUDEPATH  = .
    CONFIG  = console
    SOURCES  = main.cpp
    
  • Do "c:\path\to\msvc2017_64\bin\qmake.exe". This will create a Makefile file.

  • Run nmake. This is Microsoft MSVC's equivalent of the make tool.

  • Copy c:\path\to\msvc2017_64\bin\Qt5Core.dll into the release folder

  • Run release\example.exe. Working!


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!

  • Related