开发者

Error when including Qt Headers

I'm pretty new to Qt and I'm having a bit of trouble building with and linking the QMobility lib. I am not using qt creator for this. Here's a sample test.cpp:

#include "Qt/qapplication.h"
#include "Qt/qobject.h"
//#include <QtSystemInfo> //
#include "q开发者_如何学Csysteminfo.h"

int main()
{}

build cmd:

gcc -c test.cpp -L/usr/include/qt4/ -lQtCore -I/usr/include/qt4 -I/usr/include/QtSystemInfo

error: In file included from test.cpp:4: /usr/include/QtSystemInfo/qsysteminfo.h:51: fatal error: QObject: No such file or directory

or if I include line 3, which is commented now, it just get a file not found error.

What am I doing wrong? Thanks.


You should be including like: #include <QApplication>.

If that doesn't work, you probably have a bad installation. Try reinstalling.


Usually you create a QT Project file (something.pro) and compile using

qmake && make

in your terminal. Have you tried that?


Why do you want to hurt yourself that way :-) You'll have hard time writing a makefile to compile without using qmake, supposing you want to take advantage of the Qt meta object system (you should use the moc tool as well). Anyway, just to be clearer, I rewrote your sample like this:

#include <QApplication>
#include <QObject>
#include <QSystemInfo>

int main()
{
   return 0;
}

but you can keep it as you wrote it. To compile this, I had to use this command line:

g++ test.cpp -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtCore -I/usr/include/qt4 -I/usr/local/Trolltech/QtMobility-x86-1.1.3/include/QtSystemInfo -I/usr/local/Trolltech/QtMobility-x86-1.1.3/include/QtMobility

Of course, you'll have to change the paths according to the locations of the headers in your system. Remember that each header includes others, so you'll have to "recursively" provide all the necessary include paths. Of course you will have to link the libraries afterwards.

With a test.pro file it would have been simpler:

QT      += core gui
CONFIG  += mobility
MOBILITY    += systeminfo
TEMPLATE    = app
TARGET  = test
SOURCES     += test.cpp

and this is correctly configured with all the necessary include paths you'll need and correctly links to libraries. You can also use all the Qt extensions to C++ this way.

qmake test.pro && make

and you're done.


In a standalone Visual Studio Project I came across the same response. I had

#include <QtCore>

and the include path as c:\Qt\4.8.6\include\QtCore.

The system files had the error:

QtCore\qobject.h : No such file or directory.

I added the c:\Qt\4.8.6\include to the include path and the problem went away.


Depending on the IDE you are using, you should be able to set up a "Qt project". Then the IDE takes care of the trouble, that you and others here try to solve "by hand". CodeBlocks supports Qt-projects, MS Visual Studio, and I think a lot of other IDE's. I suggest you google "<yourIDE> Qt-project getting started". Or consider using another IDE. But you definately have to get familiar with qmake, and .PRO-files at a certain point I'm afraid.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜