2

我在带有 GCC 的 Linux 上使用 Qt 4.8.3。我正在尝试使用 QtPlugin 创建模块化应用程序。该应用程序描述了一些基本功能,然后大量插件将提供所有独特的功能。每个插件使用一个工厂来提供一个类的多个实例,并由工厂类和要提供的类组成。

目前,应用程序正在编译,但没有执行,我收到了诸如Cannot load library ~: (~: undefined symbol: _ZTI8Base16staticMetaObjectE). 我不确定如何解决此错误。删除对 Base 的所有提及可以解决问题,但是实例需要从该类派生,所以我认为这不会起作用。

我在下面的项目结构中包含了可执行文件和一个插件(它们目前都是相同的并且有相同的问题)。

非常感谢你的协助。

目录结构

main_project
+- main_project.pro
+- app/
| +- app.pro
| +- main.cpp
| +- factoryinterface.h
| +- base.h
| +- base.cpp
|
+- counter/
  +- counter.pro
  +- counter.h
  +- counterfactory.cpp
  +- counterfactory.h
  +- counter.cpp

main_project.pro

TEMPLATE = subdirs

SUBDIRS += \
    app \
    counter

应用程序.pro

QT       += core

QT       -= gui

DESTDIR = ../
TARGET = app_exec
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app

SOURCES += main.cpp \
    base.cpp

HEADERS += \
    base.h \
    factoryinterface.h

工厂接口.h

#ifndef FACTORYINTERFACE_H
#define FACTORYINTERFACE_H

#include <QtPlugin>
#include "base.h"

class FactoryInterface {
public:
    virtual ~FactoryInterface() {}
    virtual Base *get() = 0;
    virtual QString name() = 0;
};

Q_DECLARE_INTERFACE(FactoryInterface,
                    "com.example.FactoryInterface/1.0")

#endif // FACTORYINTERFACE_H

基础.h

#ifndef BASE_H
#define BASE_H

#include <QObject>

#include <QString>
#include <QStringList>
#include <QHash>

class Base : public QObject {
    Q_OBJECT
public:
    Base(QObject *parent = 0);
    virtual ~Base();

    virtual QString name();

signals:

public slots:

protected:
    QString name_;
};

#endif // BASE_H

基础.cpp

#include "base.h"

Base::Base(QObject *parent) :
    QObject(parent)
{
}

Base::~Base()
{
}

QString Base::name()
{
    return name_;
}

主文件

#include <QtCore/QCoreApplication>

#include <QDir>
#include <QPluginLoader>
#include <QDebug>
#include "factoryinterface.h"


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QDir pluginDir(a.applicationDirPath());
    pluginDir.cd("plugins");
    foreach(QString filename, pluginDir.entryList(QDir::Files))
    {
        QPluginLoader loader(pluginDir.absoluteFilePath(filename));
        QObject *plugin = loader.instance();

        if (plugin)
        {
            FactoryInterface *i = qobject_cast<FactoryInterface*>(plugin);

            if (i)
            {
                qDebug() << " :" << i->name();
            }
        } else {
            qDebug() << loader.errorString();
        }
    }

    return a.exec();
}

计数器.pro

QT       -= gui
CONFIG   += plugin

TARGET = $$qtLibraryTarget(counter)
TEMPLATE = lib

DEFINES += COUNTER_LIBRARY

SOURCES += counter.cpp \
    counterfactory.cpp

HEADERS += counter.h \
    counterfactory.h

INCLUDEPATH += ../app

DESTDIR = ../plugins

symbian {
    MMP_RULES += EXPORTUNFROZEN
    TARGET.UID3 = 0xE5B38B75
    TARGET.CAPABILITY = 
    TARGET.EPOCALLOWDLLDATA = 1
    addFiles.sources = counter.dll
    addFiles.path = !:/sys/bin
    DEPLOYMENT += addFiles
}

unix:!symbian {
    maemo5 {
        target.path = /opt/usr/lib
    } else {
        target.path = /usr/lib
    }
    INSTALLS += target
}

计数器.h

#ifndef COUNTER_H
#define COUNTER_H

#include <QObject>
#include "base.h"

class Counter : public Base {
    Q_OBJECT
public:
    Counter();
};

#endif // COUNTER_H

计数器.cpp

#include "counter.h"


Counter::Counter()
{
    name_ = "Counter";
}

反工厂.h

#ifndef COUNTER_H
#define COUNTER_H

#include <QObject>
#include "factoryinterface.h"
#include "base.h"
#include "counter.h"

class CounterFactory : public QObject, public FactoryInterface
{
    Q_OBJECT
    Q_INTERFACES(FactoryInterface)
public:
    explicit CounterFactory(QObject *parent = 0);
    Base *get();
    QString name();

signals:

public slots:

};

#endif // COUNTER_H

反工厂.cpp

#include "counterfactory.h"


CounterFactory::CounterFactory(QObject *parent) :
    QObject(parent)
{
}

Base *CounterFactory::get()
{
    return new Counter;
}

QString CounterFactory::name()
{
    return "Counter";
}

Q_EXPORT_PLUGIN2(counter, CounterFactory)

编辑:在调试模式下编译后更新错误消息。

编辑:我尝试导出/导入 BaseNode 类,但这也不起作用。我不确定这是完全不正确还是我做错了。对 Base 类进行了以下更改(将 BASE_LIBRARY 添加到 app.pro 文件中的 DEFINES 中):

#include <QtCore/qglobal.h>

#if defined(BASE_LIBRARY)
#  define BASESHARED_EXPORT Q_DECL_EXPORT
#else
#  define BASESHARED_EXPORT Q_DECL_IMPORT
#endif

class BASESHARED_EXPORT Base : public QObject {
4

1 回答 1

0

最终解决方案是将 Base 类添加到 counter.pro 文件中。我在下面列出了完整更新的 counter.pro 文件,但唯一的变化是添加了 HEADERS 和 SOURCES。

计数器.pro

QT       -= gui
CONFIG   += plugin

TARGET = $$qtLibraryTarget(counter)
TEMPLATE = lib

DEFINES += COUNTER_LIBRARY

SOURCES += counter.cpp \
    counterfactory.cpp \
    ../app/base.cpp

HEADERS += counter.h \
    counterfactory.h \
    ../app/base.h

INCLUDEPATH += ../app

DESTDIR = ../plugins

symbian {
    MMP_RULES += EXPORTUNFROZEN
    TARGET.UID3 = 0xE5B38B75
    TARGET.CAPABILITY = 
    TARGET.EPOCALLOWDLLDATA = 1
    addFiles.sources = counter.dll
    addFiles.path = !:/sys/bin
    DEPLOYMENT += addFiles
}

unix:!symbian {
    maemo5 {
        target.path = /opt/usr/lib
    } else {
        target.path = /usr/lib
    }
    INSTALLS += target
}
于 2013-01-09T15:21:49.003 回答