C++ 如何使用cmake启用qt dbus封送?
我试图创建一个非常简单的dbus封送示例,使用cmake作为构建系统。在问题的末尾可以找到完整的代码 我面临的问题是,如何在生成的适配器中包含消息头定义结构。C++ 如何使用cmake启用qt dbus封送?,c++,qt,cmake,marshalling,dbus,C++,Qt,Cmake,Marshalling,Dbus,我试图创建一个非常简单的dbus封送示例,使用cmake作为构建系统。在问题的末尾可以找到完整的代码 我面临的问题是,如何在生成的适配器中包含消息头定义结构。qdbusxml2cpp工具允许传递所需的include,但cmake的qt4\u add\u dbus\u适配器宏似乎不接受比include头更多的内容 错误是: chatadaptor.moc:59:72: error: invalid use of incomplete type ‘struct MessageData’ 我知道我可
qdbusxml2cpp
工具允许传递所需的include,但cmake的qt4\u add\u dbus\u适配器
宏似乎不接受比include头更多的内容
错误是:
chatadaptor.moc:59:72: error: invalid use of incomplete type ‘struct MessageData’
我知道我可以手动添加include,但我希望cmake为我这样做
那么,如何结合cmake启用dbus封送
我不确定这是否重要,但即使使用qmake,我也没有找到让dbus编组的方法
CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED( VERSION 2.6 )
PROJECT( CustomDbusTypes )
FIND_PACKAGE( Qt4 COMPONENTS QtCore QtGui QtDBUS REQUIRED )
include(${QT_USE_FILE})
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
SET( CustomDbusTypes_SRC
main.cpp
MyMessages.cpp
DbusMessagesReceiver.cpp
)
SET( CustomDbusTypes_HEADERS
MyMessages.hpp
DbusMessagesReceiver.hpp
)
QT4_WRAP_CPP( CustomDbusTypes_MOC ${CustomDbusTypes_HEADERS} )
SET( HEADERS_NEEDED_FOR_DBUS_ADAPTER
DbusMessagesReceiver.hpp
MyMessages.hpp
)
qt4_add_dbus_adaptor( CustomDbusTypes_ADAPTOR_SRC
com.demo.Chat.xml
DbusMessagesReceiver.hpp
DbusMessagesReceiver )
SET( CustomDbusTypes_QT_GENERATED_FILES
${CustomDbusTypes_MOC}
${CustomDbusTypes_ADAPTOR_SRC} )
add_executable( chat ${CustomDbusTypes_SRC} ${CustomDbusTypes_QT_GENERATED_FILES} )
target_link_libraries( chat
${QT_LIBRARIES}
)
#include <QApplication>
int main( int argc, char* argv[] )
{
QApplication app( argc, argv );
return app.exec();
}
#ifndef MYMESSAGES_HPP
#define MYMESSAGES_HPP
#include <QString>
#include <QMetaType>
#include <QtDBus>
struct MessageData
{
QString sender;
QString message;
};
QDBusArgument& operator<<( QDBusArgument & argument, const MessageData & data );
const QDBusArgument& operator>>( const QDBusArgument & argument, MessageData & data );
void registerCommTypes();
Q_DECLARE_METATYPE(MessageData)
#endif
#include "MyMessages.hpp"
QDBusArgument &operator<<( QDBusArgument & argument, const MessageData & data )
{
argument.beginStructure();
argument << data.sender << data.message;
argument.endStructure();
return argument;
}
const QDBusArgument &operator>>( const QDBusArgument & argument, MessageData & data )
{
argument.beginStructure();
argument >> data.sender >> data.message;
argument.endStructure();
return argument;
}
void registerCommTypes()
{
qDBusRegisterMetaType<MessageData>();
}
#ifndef DBUSMESSAGESRECEIVER_HPP
#define DBUSMESSAGESRECEIVER_HPP
#include <QObject>
#include <QString>
struct MessageData;
class DbusMessagesReceiver : public QObject
{
Q_OBJECT
public:
/// Slots called by the DBUS messaging system when a message arrives
public slots:
void OnMessageData( const MessageData & data );
};
#endif
#include "DbusMessagesReceiver.hpp"
#include "MyMessages.hpp"
#include <iostream>
void DbusMessagesReceiver::OnMessageData( const MessageData & data )
{
std::cout << data.sender.toStdString() << std::endl;
}
main.cpp:
CMAKE_MINIMUM_REQUIRED( VERSION 2.6 )
PROJECT( CustomDbusTypes )
FIND_PACKAGE( Qt4 COMPONENTS QtCore QtGui QtDBUS REQUIRED )
include(${QT_USE_FILE})
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
SET( CustomDbusTypes_SRC
main.cpp
MyMessages.cpp
DbusMessagesReceiver.cpp
)
SET( CustomDbusTypes_HEADERS
MyMessages.hpp
DbusMessagesReceiver.hpp
)
QT4_WRAP_CPP( CustomDbusTypes_MOC ${CustomDbusTypes_HEADERS} )
SET( HEADERS_NEEDED_FOR_DBUS_ADAPTER
DbusMessagesReceiver.hpp
MyMessages.hpp
)
qt4_add_dbus_adaptor( CustomDbusTypes_ADAPTOR_SRC
com.demo.Chat.xml
DbusMessagesReceiver.hpp
DbusMessagesReceiver )
SET( CustomDbusTypes_QT_GENERATED_FILES
${CustomDbusTypes_MOC}
${CustomDbusTypes_ADAPTOR_SRC} )
add_executable( chat ${CustomDbusTypes_SRC} ${CustomDbusTypes_QT_GENERATED_FILES} )
target_link_libraries( chat
${QT_LIBRARIES}
)
#include <QApplication>
int main( int argc, char* argv[] )
{
QApplication app( argc, argv );
return app.exec();
}
#ifndef MYMESSAGES_HPP
#define MYMESSAGES_HPP
#include <QString>
#include <QMetaType>
#include <QtDBus>
struct MessageData
{
QString sender;
QString message;
};
QDBusArgument& operator<<( QDBusArgument & argument, const MessageData & data );
const QDBusArgument& operator>>( const QDBusArgument & argument, MessageData & data );
void registerCommTypes();
Q_DECLARE_METATYPE(MessageData)
#endif
#include "MyMessages.hpp"
QDBusArgument &operator<<( QDBusArgument & argument, const MessageData & data )
{
argument.beginStructure();
argument << data.sender << data.message;
argument.endStructure();
return argument;
}
const QDBusArgument &operator>>( const QDBusArgument & argument, MessageData & data )
{
argument.beginStructure();
argument >> data.sender >> data.message;
argument.endStructure();
return argument;
}
void registerCommTypes()
{
qDBusRegisterMetaType<MessageData>();
}
#ifndef DBUSMESSAGESRECEIVER_HPP
#define DBUSMESSAGESRECEIVER_HPP
#include <QObject>
#include <QString>
struct MessageData;
class DbusMessagesReceiver : public QObject
{
Q_OBJECT
public:
/// Slots called by the DBUS messaging system when a message arrives
public slots:
void OnMessageData( const MessageData & data );
};
#endif
#include "DbusMessagesReceiver.hpp"
#include "MyMessages.hpp"
#include <iostream>
void DbusMessagesReceiver::OnMessageData( const MessageData & data )
{
std::cout << data.sender.toStdString() << std::endl;
}
#包括
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
返回app.exec();
}
MyMessages.hpp:
CMAKE_MINIMUM_REQUIRED( VERSION 2.6 )
PROJECT( CustomDbusTypes )
FIND_PACKAGE( Qt4 COMPONENTS QtCore QtGui QtDBUS REQUIRED )
include(${QT_USE_FILE})
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
SET( CustomDbusTypes_SRC
main.cpp
MyMessages.cpp
DbusMessagesReceiver.cpp
)
SET( CustomDbusTypes_HEADERS
MyMessages.hpp
DbusMessagesReceiver.hpp
)
QT4_WRAP_CPP( CustomDbusTypes_MOC ${CustomDbusTypes_HEADERS} )
SET( HEADERS_NEEDED_FOR_DBUS_ADAPTER
DbusMessagesReceiver.hpp
MyMessages.hpp
)
qt4_add_dbus_adaptor( CustomDbusTypes_ADAPTOR_SRC
com.demo.Chat.xml
DbusMessagesReceiver.hpp
DbusMessagesReceiver )
SET( CustomDbusTypes_QT_GENERATED_FILES
${CustomDbusTypes_MOC}
${CustomDbusTypes_ADAPTOR_SRC} )
add_executable( chat ${CustomDbusTypes_SRC} ${CustomDbusTypes_QT_GENERATED_FILES} )
target_link_libraries( chat
${QT_LIBRARIES}
)
#include <QApplication>
int main( int argc, char* argv[] )
{
QApplication app( argc, argv );
return app.exec();
}
#ifndef MYMESSAGES_HPP
#define MYMESSAGES_HPP
#include <QString>
#include <QMetaType>
#include <QtDBus>
struct MessageData
{
QString sender;
QString message;
};
QDBusArgument& operator<<( QDBusArgument & argument, const MessageData & data );
const QDBusArgument& operator>>( const QDBusArgument & argument, MessageData & data );
void registerCommTypes();
Q_DECLARE_METATYPE(MessageData)
#endif
#include "MyMessages.hpp"
QDBusArgument &operator<<( QDBusArgument & argument, const MessageData & data )
{
argument.beginStructure();
argument << data.sender << data.message;
argument.endStructure();
return argument;
}
const QDBusArgument &operator>>( const QDBusArgument & argument, MessageData & data )
{
argument.beginStructure();
argument >> data.sender >> data.message;
argument.endStructure();
return argument;
}
void registerCommTypes()
{
qDBusRegisterMetaType<MessageData>();
}
#ifndef DBUSMESSAGESRECEIVER_HPP
#define DBUSMESSAGESRECEIVER_HPP
#include <QObject>
#include <QString>
struct MessageData;
class DbusMessagesReceiver : public QObject
{
Q_OBJECT
public:
/// Slots called by the DBUS messaging system when a message arrives
public slots:
void OnMessageData( const MessageData & data );
};
#endif
#include "DbusMessagesReceiver.hpp"
#include "MyMessages.hpp"
#include <iostream>
void DbusMessagesReceiver::OnMessageData( const MessageData & data )
{
std::cout << data.sender.toStdString() << std::endl;
}
\ifndef MYMESSAGES\u水电站
#定义MYMESSAGES\u HPP
#包括
#包括
#包括
结构消息数据
{
QString发送器;
QString消息;
};
QDBusArgument和运算符(常量QDBusArgument和argument、MessageData和data);
void registerCommTypes();
Q_DECLARE_元类型(MessageData)
#恩迪夫
MyMessages.cpp:
CMAKE_MINIMUM_REQUIRED( VERSION 2.6 )
PROJECT( CustomDbusTypes )
FIND_PACKAGE( Qt4 COMPONENTS QtCore QtGui QtDBUS REQUIRED )
include(${QT_USE_FILE})
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
SET( CustomDbusTypes_SRC
main.cpp
MyMessages.cpp
DbusMessagesReceiver.cpp
)
SET( CustomDbusTypes_HEADERS
MyMessages.hpp
DbusMessagesReceiver.hpp
)
QT4_WRAP_CPP( CustomDbusTypes_MOC ${CustomDbusTypes_HEADERS} )
SET( HEADERS_NEEDED_FOR_DBUS_ADAPTER
DbusMessagesReceiver.hpp
MyMessages.hpp
)
qt4_add_dbus_adaptor( CustomDbusTypes_ADAPTOR_SRC
com.demo.Chat.xml
DbusMessagesReceiver.hpp
DbusMessagesReceiver )
SET( CustomDbusTypes_QT_GENERATED_FILES
${CustomDbusTypes_MOC}
${CustomDbusTypes_ADAPTOR_SRC} )
add_executable( chat ${CustomDbusTypes_SRC} ${CustomDbusTypes_QT_GENERATED_FILES} )
target_link_libraries( chat
${QT_LIBRARIES}
)
#include <QApplication>
int main( int argc, char* argv[] )
{
QApplication app( argc, argv );
return app.exec();
}
#ifndef MYMESSAGES_HPP
#define MYMESSAGES_HPP
#include <QString>
#include <QMetaType>
#include <QtDBus>
struct MessageData
{
QString sender;
QString message;
};
QDBusArgument& operator<<( QDBusArgument & argument, const MessageData & data );
const QDBusArgument& operator>>( const QDBusArgument & argument, MessageData & data );
void registerCommTypes();
Q_DECLARE_METATYPE(MessageData)
#endif
#include "MyMessages.hpp"
QDBusArgument &operator<<( QDBusArgument & argument, const MessageData & data )
{
argument.beginStructure();
argument << data.sender << data.message;
argument.endStructure();
return argument;
}
const QDBusArgument &operator>>( const QDBusArgument & argument, MessageData & data )
{
argument.beginStructure();
argument >> data.sender >> data.message;
argument.endStructure();
return argument;
}
void registerCommTypes()
{
qDBusRegisterMetaType<MessageData>();
}
#ifndef DBUSMESSAGESRECEIVER_HPP
#define DBUSMESSAGESRECEIVER_HPP
#include <QObject>
#include <QString>
struct MessageData;
class DbusMessagesReceiver : public QObject
{
Q_OBJECT
public:
/// Slots called by the DBUS messaging system when a message arrives
public slots:
void OnMessageData( const MessageData & data );
};
#endif
#include "DbusMessagesReceiver.hpp"
#include "MyMessages.hpp"
#include <iostream>
void DbusMessagesReceiver::OnMessageData( const MessageData & data )
{
std::cout << data.sender.toStdString() << std::endl;
}
#包括“MyMessages.hpp”
QDBusArgument&operator data.sender>>data.message;
argument.endStructure();
返回参数;
}
无效注册表命令类型()
{
qDBusRegisterMetaType();
}
DbusMessagesReceiver.hpp:
CMAKE_MINIMUM_REQUIRED( VERSION 2.6 )
PROJECT( CustomDbusTypes )
FIND_PACKAGE( Qt4 COMPONENTS QtCore QtGui QtDBUS REQUIRED )
include(${QT_USE_FILE})
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
SET( CustomDbusTypes_SRC
main.cpp
MyMessages.cpp
DbusMessagesReceiver.cpp
)
SET( CustomDbusTypes_HEADERS
MyMessages.hpp
DbusMessagesReceiver.hpp
)
QT4_WRAP_CPP( CustomDbusTypes_MOC ${CustomDbusTypes_HEADERS} )
SET( HEADERS_NEEDED_FOR_DBUS_ADAPTER
DbusMessagesReceiver.hpp
MyMessages.hpp
)
qt4_add_dbus_adaptor( CustomDbusTypes_ADAPTOR_SRC
com.demo.Chat.xml
DbusMessagesReceiver.hpp
DbusMessagesReceiver )
SET( CustomDbusTypes_QT_GENERATED_FILES
${CustomDbusTypes_MOC}
${CustomDbusTypes_ADAPTOR_SRC} )
add_executable( chat ${CustomDbusTypes_SRC} ${CustomDbusTypes_QT_GENERATED_FILES} )
target_link_libraries( chat
${QT_LIBRARIES}
)
#include <QApplication>
int main( int argc, char* argv[] )
{
QApplication app( argc, argv );
return app.exec();
}
#ifndef MYMESSAGES_HPP
#define MYMESSAGES_HPP
#include <QString>
#include <QMetaType>
#include <QtDBus>
struct MessageData
{
QString sender;
QString message;
};
QDBusArgument& operator<<( QDBusArgument & argument, const MessageData & data );
const QDBusArgument& operator>>( const QDBusArgument & argument, MessageData & data );
void registerCommTypes();
Q_DECLARE_METATYPE(MessageData)
#endif
#include "MyMessages.hpp"
QDBusArgument &operator<<( QDBusArgument & argument, const MessageData & data )
{
argument.beginStructure();
argument << data.sender << data.message;
argument.endStructure();
return argument;
}
const QDBusArgument &operator>>( const QDBusArgument & argument, MessageData & data )
{
argument.beginStructure();
argument >> data.sender >> data.message;
argument.endStructure();
return argument;
}
void registerCommTypes()
{
qDBusRegisterMetaType<MessageData>();
}
#ifndef DBUSMESSAGESRECEIVER_HPP
#define DBUSMESSAGESRECEIVER_HPP
#include <QObject>
#include <QString>
struct MessageData;
class DbusMessagesReceiver : public QObject
{
Q_OBJECT
public:
/// Slots called by the DBUS messaging system when a message arrives
public slots:
void OnMessageData( const MessageData & data );
};
#endif
#include "DbusMessagesReceiver.hpp"
#include "MyMessages.hpp"
#include <iostream>
void DbusMessagesReceiver::OnMessageData( const MessageData & data )
{
std::cout << data.sender.toStdString() << std::endl;
}
\ifndef DBUSMESSAGESRECEIVER\u水电站
#定义DBUSMESSAGESRECEIVER\u HPP
#包括
#包括
结构消息数据;
类DbusMessagesReceiver:公共QObject
{
Q_对象
公众:
///消息到达时DBUS消息传递系统调用的插槽
公众时段:
无效的消息数据(const MessageData和data);
};
#恩迪夫
DbusMessagesReceiver.cpp:
CMAKE_MINIMUM_REQUIRED( VERSION 2.6 )
PROJECT( CustomDbusTypes )
FIND_PACKAGE( Qt4 COMPONENTS QtCore QtGui QtDBUS REQUIRED )
include(${QT_USE_FILE})
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
SET( CustomDbusTypes_SRC
main.cpp
MyMessages.cpp
DbusMessagesReceiver.cpp
)
SET( CustomDbusTypes_HEADERS
MyMessages.hpp
DbusMessagesReceiver.hpp
)
QT4_WRAP_CPP( CustomDbusTypes_MOC ${CustomDbusTypes_HEADERS} )
SET( HEADERS_NEEDED_FOR_DBUS_ADAPTER
DbusMessagesReceiver.hpp
MyMessages.hpp
)
qt4_add_dbus_adaptor( CustomDbusTypes_ADAPTOR_SRC
com.demo.Chat.xml
DbusMessagesReceiver.hpp
DbusMessagesReceiver )
SET( CustomDbusTypes_QT_GENERATED_FILES
${CustomDbusTypes_MOC}
${CustomDbusTypes_ADAPTOR_SRC} )
add_executable( chat ${CustomDbusTypes_SRC} ${CustomDbusTypes_QT_GENERATED_FILES} )
target_link_libraries( chat
${QT_LIBRARIES}
)
#include <QApplication>
int main( int argc, char* argv[] )
{
QApplication app( argc, argv );
return app.exec();
}
#ifndef MYMESSAGES_HPP
#define MYMESSAGES_HPP
#include <QString>
#include <QMetaType>
#include <QtDBus>
struct MessageData
{
QString sender;
QString message;
};
QDBusArgument& operator<<( QDBusArgument & argument, const MessageData & data );
const QDBusArgument& operator>>( const QDBusArgument & argument, MessageData & data );
void registerCommTypes();
Q_DECLARE_METATYPE(MessageData)
#endif
#include "MyMessages.hpp"
QDBusArgument &operator<<( QDBusArgument & argument, const MessageData & data )
{
argument.beginStructure();
argument << data.sender << data.message;
argument.endStructure();
return argument;
}
const QDBusArgument &operator>>( const QDBusArgument & argument, MessageData & data )
{
argument.beginStructure();
argument >> data.sender >> data.message;
argument.endStructure();
return argument;
}
void registerCommTypes()
{
qDBusRegisterMetaType<MessageData>();
}
#ifndef DBUSMESSAGESRECEIVER_HPP
#define DBUSMESSAGESRECEIVER_HPP
#include <QObject>
#include <QString>
struct MessageData;
class DbusMessagesReceiver : public QObject
{
Q_OBJECT
public:
/// Slots called by the DBUS messaging system when a message arrives
public slots:
void OnMessageData( const MessageData & data );
};
#endif
#include "DbusMessagesReceiver.hpp"
#include "MyMessages.hpp"
#include <iostream>
void DbusMessagesReceiver::OnMessageData( const MessageData & data )
{
std::cout << data.sender.toStdString() << std::endl;
}
#包括“DbusMessagesReceiver.hpp”
#包括“MyMessages.hpp”
#包括
void DbusMessagesReceiver::OnMessageData(const MessageData&data)
{
我终于明白了,这很容易
问题在于,我在头文件(确切地说是DbusMessagesReceiver.hpp)中转发了MessageData
结构,该头文件包含在生成的源文件中
一旦我将包含MessageData
结构定义的标题包含在此标题中,问题就消失了
因此,解决方案是将DbusMessagesReceiver.hpp更改为:
#ifndef DBUSMESSAGESRECEIVER_HPP
#define DBUSMESSAGESRECEIVER_HPP
#include <QObject>
#include <QString>
#include "MyMessages.hpp"
class DbusMessagesReceiver : public QObject
{
Q_OBJECT
public:
/// Slots called by the DBUS messaging system when a message arrives
public slots:
void OnMessageData( const MessageData & data );
};
#endif
\ifndef DBUSMESSAGESRECEIVER\u水电站
#定义DBUSMESSAGESRECEIVER\u HPP
#包括
#包括
#包括“MyMessages.hpp”
类DbusMessagesReceiver:公共QObject
{
Q_对象
公众:
///消息到达时DBUS消息传递系统调用的插槽
公众时段:
无效的消息数据(const MessageData和data);
};
#恩迪夫
cmake开发人员非常聪明-可惜没有记录:(