How to compile a Qt program without qtCreator on Windows?
Asked Answered
S

1

0

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?

Sturrock answered 27/6, 2022 at 15:7 Comment(4)
If the file is not present, how do you expect cl.exe to include it?Garrott
Of course @Anders, so the question is how to optimally set the cl.exe includes and lib parameters to avoid to include every single subdirectory of qt as /I parameters manually. Is there a single include directive that would automatically work (with relative paths for example)?Sturrock
Do you have the header file on your system or not?Garrott
@Garrott qtbase-everywhere-opensource-src-5.15.4.zip seem to contain them in the include subfolder, so I guess yes.Sturrock
S
0

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!

Sturrock answered 27/6, 2022 at 21:4 Comment(5)
What's the motivation to avoid qmake? If you have Qt installed, you'll have qmake installed, so you might as well use it... and if you don't have Qt installed, your program won't compile anyway, since the Qt headers and libraries won't be available.Geezer
@JeremyFriesner I initially thought it would be possible to compile a Qt project with only: MSVC (cl.exe, etc.) + Qt headers (cl.exe /I) + Qt lib (cl.exe /link /libpath), and without anything else. Is this possible? The reasoning was: I usually don't want my tooling (make/build system, text editor, etc.) to be modified each time I use a new library. (I initially preferred not to change my current build system to qmake, and my text editor to QtCreator IDE)Sturrock
sure it's possible; just take the project files and/or Makefiles emitted by qmake, and use them as if you had written them yourself. It's going to be a pain to maintain though, since every time you need to add another file to your project or change a setting, or whatever, you're going to have to manually update those project files in the correct way, and if later you want to compiler your program for a different compiler or OS, you're going to have to create and maintain separate makefiles/project files for that OS too. It's much easier to simply maintain a single .pro file, though.Geezer
Btw I've used Qt without QtCreator for 10+ years now; QtCreator isn't required to program in Qt. (I just use vim for a text editor, and the bash shell or DOS prompt). Just make a .pro file and run "qmake" from the command line, then you can compile by running the generated makefile, just like with any other program.Geezer
@JeremyFriesner You're right: my own text editor (and not QtCreator) + qmake is probably the best compromise on my case. Feel free to post tour two last comments as an answer, it would be useful for future reference/future readers.Sturrock

© 2022 - 2024 — McMap. All rights reserved.