I am trying to create a very simple example of dbus marshaling, using cmake as the build system. Complete code is found at the end of the question.
The problem I am facing, is how to include the header defining structures for the messages in the generated adapter. The qdbusxml2cpp
tool allows passing required includes, but cmake's qt4_add_dbus_adaptor
macro doesn't seems to accept more then include header.
The error is :
chatadaptor.moc:59:72: error: invalid use of incomplete type ‘struct MessageData’
I know I could manually add the include, but I want cmake to do it for me.
So, how can I enable dbus marshaling combined with cmake?
I am not sure if it matters, but I haven't found a way to have dbus marshaling even with qmake.
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}
)
main.cpp :
#include <QApplication>
int main( int argc, char* argv[] )
{
QApplication app( argc, argv );
return app.exec();
}
MyMessages.hpp :
#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
MyMessages.cpp :
#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>();
}
DbusMessagesReceiver.hpp :
#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
DbusMessagesReceiver.cpp :
#include "DbusMessagesReceiver.hpp"
#include "MyMessages.hpp"
#include <iostream>
void DbusMessagesReceiver::OnMessageData( const MessageData & data )
{
std::cout << data.sender.toStdString() << std::endl;
}
Finally, I figured out, and it is quite easy.
The problem was that I forward declared the MessageData
structure in the header file (in DbusMessagesReceiver.hpp to be precise) that is included in the generated source file.
Once I included the header including the definition of the MessageData
structure in this header, the problem disappeared.
So, the solution is to change DbusMessagesReceiver.hpp
to this:
#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
Pretty smart from cmake developers - too bad it is not documented :(
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.