0

I try to learn C++ with Qt following examples from a book. On very first example including Qt I have a compilation error:

$ g++ -Wall qtdemo.cpp
qtdemo.cpp:1:19: fatal error: QString: No such file or directory
compilation terminated.

The g++ works fine if I don't include any Qt library. I try to run the same program using Qt Creator and it works if I do a "Qt Console application" but shows error if I do a "Plain C++ application". I tried to give a path to QString after g++ command, different configuration etc. It seems g++ can't see Qt at all. I even installed Qt5 on top of already existing on Ubuntu 16.04 Qt4. When I check if I have Qt installed it looks everything should be fine:

$ locate QString
/opt/Qt5.7.0/5.7/Src/qtbase/include/QtCore/QString
...
/opt/Qt5.7.0/5.7/gcc_64/include/QtCore/QString
...
/usr/include/qt4/QtCore/QString
...

$ qmake -v
QMake version 2.01a
Using Qt version 4.8.7 in /usr/lib/x86_64-linux-gnu

The code I try to run:

#include <QString>
#include <QTextStream>

QTextStream cout(stdout);
QTextStream cin(stdin);

int main() {
        QString s1("This "), s2("is s "), s3("string");
        s1 += s2; //concatenation
        QString s4 = s1+s3;
        cout << s4 << endl;
        cout << "The length of that string is " << s4.length() << endl;
        cout << "Enter a sentence with withspace: " << endl;
        s2 = cin.readLine();
        cout << "Here is your sentence: \n" << s2 << endl;
        cout << "The length of your sentnce is: " << s2.length() << endl;
        return 0;
}

I don't know how to make Qt application works from command line with g++ and where the error comes from.

hyde
  • 60,639
  • 21
  • 115
  • 176
tomasz74
  • 16,031
  • 10
  • 37
  • 51
  • Stuff under /opt isn't (normally) in the usual library or include file search paths of linkers, so you have to give appropriate compiler options. But, just use *qmake* or *qbs* to manage your Qt project, because you don't want to be doing this stuff by hand... – hyde Aug 16 '16 at 10:57

2 Answers2

3

For building qt projects (which can also be c++ projects containing qt libs) I would strongly advise to look at the qmake tools (e.g. http://doc.qt.io/qt-4.8/qmake-manual.html for qt4.8 or http://doc.qt.io/qt-5/qmake-manual.html for qt5).

As the manual says

qmake is a tool that helps simplify the build process for development project across different platforms

For your code the only thing I needed to do to compile was to put the code into a main.cpp file and then I did

qmake -project
qmake
make

and got a working executable.

During the make command you will also see the full command that it executed which will be along the lines of

g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -I. -I. -I/usr/include/qt5 -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -o main.o main.cc

which contains various include paths as well as other things, but unless you have very specific things to do I would (personally) refrain from doing things by hand.

Even if you have specific customization to do, this can be done via the .pro file that is created when doing qmake -project. You can look at the documentation for these types of files

Let me know if this helps

Erik
  • 2,137
  • 3
  • 25
  • 42
-1

The compiler has to know the Qt libraries and then after compilation you have to link against them. Instead of g++ -Wall qtdemo.cpp try at least g++ -Wall -lQtGui -lQtCore -lpthread qtdemo.cpp

If -lQtGui -lQtCore -lpthread are unknown you have to set the path with the -I option

See this thread for a working solution, especially the qmake makefile that Quent42340 posted:

Can I use Qt without qmake or Qt Creator?

Community
  • 1
  • 1
ralf htp
  • 9,149
  • 4
  • 22
  • 34