如何告诉qmake在从dbus xml生成文件时包含标头

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

本文关键字:文件 包含标 xml 何告诉 qmake 在从 dbus      更新时间:2023-10-16

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

完整示例和更多信息如下。


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

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

专业文件 (ex.pro( 如下所示:

TEMPLATE = lib
CONFIG += c++14 warn_on
QT += dbus
DBUS_INTERFACES += custom.xml
HEADERS += Info.hpp
SOURCES += Info.cpp

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

和源文件 (信息.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 文件中使用什么选项,因此生成的头文件包含 Info.hpp?

我知道qdbusxml2cpp包含一个带有-i选项的标题。如何告诉qmake去做?

这是QTBUG-11677。它已从Qt 5.0开始修复。有两种方法:

  1. 每个文件组 - 建议:

    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>,因为这会将项目配置错误推迟到链接时间。直接包含<QClass> - 或者为了方便起见,整个模块<QtModule>。如果文件无法编译,则生成文件已过时,您需要重新运行 qmake。