当从 dbus xml 生成文件时,如何告诉 qmake 包含 headers?

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

TLDR:如何告诉 qmake 使用 dbus xml 描述生成文件,其中包括适当的 header?

下面是完整示例和更多信息。


我的 xml 文件 (custom.xml) 描述 dbus 接口如下所示:

<?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>

pro 文件 (ex.pro) 如下所示:

TEMPLATE = lib

CONFIG += c++14 warn_on

QT += dbus

DBUS_INTERFACES += custom.xml

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

header 文件 (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

和源文件(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;
}

当我尝试创建 Makefile 并构建库时,它失败了,因为生成的文件中的类型是未知的,因为生成的文件不包括 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

我在pro文件中使用了什么选项,所以生成的header文件包含Info.hpp?

我知道 qdbusxml2cpp 包括 header 和 -i 选项。如何告诉qmake去做?

这是 QTBUG-11677。自 Qt 5.0 起已修复。有两种方法:

  1. Per-file组-推荐:

    custom_interface.files = custom.xml
    custom_interface.header_flags = -i Info.hpp
    DBUS_INTERFACES += custom_interface
    
  2. 为所有接口头文件设置全局选项 - 这一点都不好,因为它会用可能不相关的类型污染所有生成的接口头文件。

    QDBUSXML2CPP_INTERFACE_HEADER_FLAGS += -i Info.hpp
    

此外 - 您不应包含 <QtModule/QClass>,因为这会将项目配置错误推迟到 link 时间。为了方便起见,直接包含 <QClass> 或整个模块 <QtModule>。如果文件无法编译,则 makefile 已过时,您需要 re-run qmake。