Search code examples
c++qtmarshallingqmakeqtdbus

How to tell to qmake to include headers, when generating files from dbus xml?


TLDR: How do I tell qmake to generate files using dbus xml description, which include appropriate header?

Full example and more info follows bellow.


My xml file (custom.xml) describing dbus interface looks like this :

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE node PUBLIC
  "-//freedesktop//DTD D-Bus Object Introspection 1.0//EN"
  "http://standards.freedesktop.org/dbus/1.0/introspect.dtd">

<node>
  <interface name="com.my.custom">

    <method name="Get">
      <arg type="a(sss)" name="info" direction="out"/>
      <annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="InfoArray"/>
    </method>

  </interface>
</node>

The pro file (ex.pro) looks like this:

TEMPLATE = lib

CONFIG += c++14 warn_on

QT += dbus

DBUS_INTERFACES += custom.xml

HEADERS += Info.hpp
SOURCES += Info.cpp

The header file (Info.hpp):

#ifndef INFO_HPP
#define INFO_HPP


#include <QtDBus/QtDBus>
#include <QtCore/QString>
#include <QtCore/QList>
#include <QtCore/QMetaType>


class Info
{
public:

    friend const QDBusArgument &operator>>( const QDBusArgument &argument, Info& data );
    friend QDBusArgument &operator<<( QDBusArgument &argument, const Info& data );


    QString a;
    QString b;
    QString c;
};

typedef QList< Info > InfoArray;

Q_DECLARE_METATYPE(Info);
Q_DECLARE_METATYPE(InfoArray);

inline void registerCommType()
{
    qDBusRegisterMetaType<Info>();
    qDBusRegisterMetaType< InfoArray >();
}

#endif

and the source file (Info.cpp):

#include "Info.hpp"

QDBusArgument& operator<<( QDBusArgument& argument,
                           const Info& data )
{
    argument.beginStructure();
        argument << data.a;
        argument << data.b;
        argument << data.c;
    argument.endStructure();

    return argument;
}

const QDBusArgument& operator>>( const QDBusArgument& argument,
                                 Info& data )
{
    argument.beginStructure();
        argument >> data.a;
        argument >> data.b;
        argument >> data.c;
    argument.endStructure();

    return argument;
}

When I try to create the Makefile and build the library, it fails, because the type in the generated files is unknown, since the generated files do not include Info.hpp :

dejovivl@DEECLU52:~/workspace/qtdbus_custom$ qmake
Info: creating stash file /home/dejovivl/workspace/qtdbus_custom/.qmake.stash
dejovivl@DEECLU52:~/workspace/qtdbus_custom$ make
arm-pdm3-linux-gnueabi-g++  -march=armv7-a -marm -mfpu=neon  -mfloat-abi=hard -mcpu=cortex-a9 --sysroot=/opt/2017.12.6_2-cc/sysroots/cortexa9hf-neon-pdm3-linux-gnueabi -c -pipe  -O2 -pipe -g -feliminate-unused-debug-types  --sysroot=/opt/2017.12.6_2-cc/sysroots/cortexa9hf-neon-pdm3-linux-gnueabi -O2 -std=gnu++1y -Wall -W -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_DBUS_LIB -DQT_CORE_LIB -I. -isystem /opt/2017.12.6_2-cc/sysroots/cortexa9hf-neon-pdm3-linux-gnueabi/usr/include/qt5 -isystem /opt/2017.12.6_2-cc/sysroots/cortexa9hf-neon-pdm3-linux-gnueabi/usr/include/qt5/QtGui -isystem /opt/2017.12.6_2-cc/sysroots/cortexa9hf-neon-pdm3-linux-gnueabi/usr/include/qt5/QtDBus -isystem /opt/2017.12.6_2-cc/sysroots/cortexa9hf-neon-pdm3-linux-gnueabi/usr/include/qt5/QtCore -I. -I/opt/2017.12.6_2-cc/sysroots/cortexa9hf-neon-pdm3-linux-gnueabi/usr/lib/qt5/mkspecs/linux-oe-g++ -o Info.o Info.cpp
/opt/2017.12.6_2-cc/sysroots/x86_64-pdm3sdk-linux/usr/bin/qt5/qdbusxml2cpp -p custom_interface.h: custom.xml
/opt/2017.12.6_2-cc/sysroots/x86_64-pdm3sdk-linux/usr/bin/qt5/qdbusxml2cpp -i custom_interface.h -p :custom_interface.cpp custom.xml
arm-pdm3-linux-gnueabi-g++  -march=armv7-a -marm -mfpu=neon  -mfloat-abi=hard -mcpu=cortex-a9 --sysroot=/opt/2017.12.6_2-cc/sysroots/cortexa9hf-neon-pdm3-linux-gnueabi -c -pipe  -O2 -pipe -g -feliminate-unused-debug-types  --sysroot=/opt/2017.12.6_2-cc/sysroots/cortexa9hf-neon-pdm3-linux-gnueabi -O2 -std=gnu++1y -Wall -W -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_DBUS_LIB -DQT_CORE_LIB -I. -isystem /opt/2017.12.6_2-cc/sysroots/cortexa9hf-neon-pdm3-linux-gnueabi/usr/include/qt5 -isystem /opt/2017.12.6_2-cc/sysroots/cortexa9hf-neon-pdm3-linux-gnueabi/usr/include/qt5/QtGui -isystem /opt/2017.12.6_2-cc/sysroots/cortexa9hf-neon-pdm3-linux-gnueabi/usr/include/qt5/QtDBus -isystem /opt/2017.12.6_2-cc/sysroots/cortexa9hf-neon-pdm3-linux-gnueabi/usr/include/qt5/QtCore -I. -I/opt/2017.12.6_2-cc/sysroots/cortexa9hf-neon-pdm3-linux-gnueabi/usr/lib/qt5/mkspecs/linux-oe-g++ -o custom_interface.o custom_interface.cpp
In file included from custom_interface.cpp:12:0:
custom_interface.h:39:30: error: ‘InfoArray’ was not declared in this scope
     inline QDBusPendingReply<InfoArray> Get()
                              ^~~~~~~~~
custom_interface.h:39:39: error: template argument 1 is invalid
     inline QDBusPendingReply<InfoArray> Get()
                                       ^
custom_interface.h: In member function ‘int ComMyCustomInterface::Get()’:
custom_interface.h:42:77: error: cannot convert ‘QDBusPendingCall’ to ‘int’ in return
         return asyncCallWithArgumentList(QStringLiteral("Get"), argumentList);
                                                                             ^
Makefile:663: recipe for target 'custom_interface.o' failed
make: *** [custom_interface.o] Error 1

What option do I use in the pro file, so the generated header file include Info.hpp?

I know that qdbusxml2cpp include a header with -i option. How to tell to qmake to do it?


Solution

  • This was QTBUG-11677. It has been fixed since Qt 5.0. There are two approaches:

    1. Per-file group - recommended:

      custom_interface.files = custom.xml
      custom_interface.header_flags = -i Info.hpp
      DBUS_INTERFACES += custom_interface
      
    2. Set global options for all interface header files - this is not nice at all, since it pollutes all generated interface headers with likely irrelevant types.

      QDBUSXML2CPP_INTERFACE_HEADER_FLAGS += -i Info.hpp
      

    Also - you should not include <QtModule/QClass>, since this defers project configuration errors to link time. Include <QClass> directly - or the entire module <QtModule> for convenience's sake. If the file won't compile, then the makefile is stale and you need to re-run qmake.