QLibrary resolve returns false

QLibrary resolve returns false

本文关键字:false returns resolve QLibrary      更新时间:2023-10-16

我现在正试图动态地包含类文件,并选择通过将.dll加载到QLibrary中来这样做。我现在遇到的问题是,当我尝试调用resolve()方法时,它返回0.

编辑:与此同时,问题已经解决,我决定编辑代码,这样其他人就可以看到它是如何工作的:

这是.dll的头文件:

#ifndef DIVFIXTURE_H
#define DIVFIXTURE_H
#include<QObject>
#include<QVariant>
class __declspec(dllexport) DivFixture : public QObject
{
    Q_OBJECT
public:
    Q_INVOKABLE DivFixture();
    Q_INVOKABLE void setNumerator(QVariant num);
    Q_INVOKABLE void setDenominator(QVariant denom);
    Q_INVOKABLE QVariant quotient();
private:
    double numerator, denominator;
};
#endif

这是dll的。pcp文件:

#include "testfixture.h"
DivFixture::DivFixture(){}
void DivFixture::setNumerator(QVariant num)
{
    numerator=num.toDouble();
}

void DivFixture::setDenominator(QVariant denom)
{
    denominator=denom.toDouble();
}

QVariant DivFixture::quotient()
{
    QVariant ret;
    ret=numerator/denominator;
    return ret;
}
//non-class function to return pointer to class
extern "C" __declspec(dllexport) DivFixture* create()
{
   return new DivFixture();
}

这是我加载类的方式:

currentFixture.setFileName("C:\somepath\testFixture.dll");
if(currentFixture.load());
{
    typedef QObject* (*getCurrentFixture)();
    getCurrentFixture fixture=(getCurrentFixture)currentFixture.resolve("create");
    if (fixture)
    {
        Fixture=fixture();
    }
}

您需要使用__declspec(dllexport)导出您的类

class __declspec(dllexport) DivFixture : public QObject
{

接受的答案不正确。__declspec有两个可能的参数:

  • dllexport
  • dllimport

编译库时使用dllexport,链接库时使用dllimport。

Qt已经提供了相应的定义:

  • Q_DECL_EXPORT
  • Q_DECL_IMPORT

要正确使用它们,请添加如下内容:

#if defined(MYSHAREDLIB_LIBRARY)
#  define MYSHAREDLIB_EXPORT Q_DECL_EXPORT
#else
#  define MYSHAREDLIB_EXPORT Q_DECL_IMPORT
#endif

设置为项目中的全局标头,该全局标头将包含在要导出的所有类中。然后修改你的类,使最后的声明看起来像:

class MYSHAREDLIB_EXPORT DivFixture : public QObject

在Qt的文档中创建共享库给出了一个完整的示例和更多信息。