john
john

Reputation: 97

Compiling error c++ with QXmlDefaultHandler in linux

I think this problem is just simple but I am not really familiar how to setup my environment because someone did it for me. I think it has something to do with the Makefile. Pardon my ignorance for this. I am not using the Qt IDE. The error I encountered is this:

g++ -I /usr/include/qt4   -c -o main.o main.cpp
In file included from main.cpp:3:0:
xxml.h:4:30: fatal error: QXmlDefaultHandler: No such file or directory
 #include <QXmlDefaultHandler>
                              ^
compilation terminated.
make: *** [main.o] Error 1

my xxml.h looks like this:

#ifndef XXML_H
#define XXML_H

#include <QXmlDefaultHandler>

class XXml : public QXmlDefaultHandler
{
public:
    XXml();
    virtual ~XXml();
    bool startElement(const QString &, const QString &, const QString &, 
                        const QXmlAttributes &);

private:    

};

#endif

Makefile:

TARGET=x
OBJS=main.o xxml.o
QT_LIBS=-lQtGui -lQtCore
QT_LIBDIR=/usr/lib/i386-linux-gnu
QT_INCDIR=/usr/include/qt4

CXXFLAGS=-I $(QT_INCDIR)

$(TARGET): $(OBJS)
    g++ $^ -L $(QT_LIBDIR) $(QT_LIBS) -lpthread -o $@

moc_%.cpp: %.h
    /usr/lib/i386-linux-gnu/qt4/bin/moc -o $@ $^

clean:
    rm $(TARGET) $(OBJS)

Upvotes: 0

Views: 787

Answers (2)

Olaf Dietsche
Olaf Dietsche

Reputation: 74088

You can search for the missing header file by

find /usr/include/qt4 -iname '*QXmlDefaultHandler*'

The output will be something like

/usr/include/qt4/QtXml/QXmlDefaultHandler

As you see, the header file is in a subdirectory called QtXml. When you prefix the include, it should compile, e.g.

#include <QtXml/QXmlDefaultHandler>

QXmlDefaultHandler belongs to QtXml. To resolve the linker error, you must add the appropriate libraries to QT_LIBS. To find out which are needed, say

pkg-config --libs QtXml

which will show you

-lQtXml -lQtCore

You alread have libQtCore, so just add -lQtXml to the front

QT_LIBS=-lQtXml -lQtGui -lQtCore

As @MykhayloKopytonenko suggested, it is better to add the appropriate include paths. pkg-config delivers the necessary compiler flags for this as well, just enter

pkg-config --cflags QtXml

and you get (at least on my system)

-DQT_SHARED -I/usr/include/qt4 -I/usr/include/qt4/QtXml -I/usr/include/qt4/QtCore

To add this to your Makefile

QT_CFLAGS = -DQT_SHARED -I/usr/include/qt4 -I/usr/include/qt4/QtXml -I/usr/include/qt4/QtCore
CXXFLAGS = $(QT_CFLAGS)

If you want just the include flags, use --cflags-only-I instead

pkg-config --cflags-only-I QtXml

Upvotes: 1

john
john

Reputation: 97

Finally solved it!

adding #include <QtXml/QXmlDefaultHandler> on xxml.h and adding -lQtXml on QT_LIBS on Makefile did the trick.

Thanks everyone! :)

Upvotes: 0

Related Questions