【问题标题】:Loading library with QLibrary使用 QLibrary 加载库
【发布时间】:2017-07-15 14:12:36
【问题描述】:

我找到了以下用于在 Qt 中加载库的代码,但我不完全理解它是如何工作的。有人可以向我解释一下:typedef int (*MyPrototype)(int, int);吗?

int r1 = 0;
QLibrary library("mathlib.so");
    if (!library.load())
        out << library.errorString() << endl;
    if (library.load())
        out << "library loaded" << endl;

    typedef int (*MyPrototype)(int, int);

    MyPrototype myFunction = (MyPrototype)library.resolve("add");
    if (myFunction)
        r1 = myFunction(a,b);
    else
        out << library.errorString() << endl;

【问题讨论】:

  • 它是一个指向函数的指针。
  • 所以我必须为我想调用的每个函数都这样做?
  • 是的,你必须这样做。
  • 所以我唯一的其他选择是在应用程序中使用库时让所有头文件可用?
  • 如果你有dll,你可以生成.lib文件。

标签: c++ qt shared-libraries qlibrary


【解决方案1】:

so 或者 dll 有函数,我们想使用它,那么我们怎么调用它

int add(int in_iParam1, int in_iParam2)

定义函数类型

typedef int (*MyPrototype)(int, int);

在so文件中寻找函数'add'

MyPrototype myFunction = (MyPrototype)library.resolve("add");

使用参数'a'和'b'调用函数'add'并得到结果到'r1'

r1 = myFunction(a,b);

【讨论】:

  • Prototype 的定义是否代表任何具体的东西?
  • 它应该包含与你将调用的函数相同的类型
  • 好的,但我指的是函数定义的实际名称:MyPrototype?
  • 是的,在函数类型定义中可以使用任何名称
猜你喜欢
  • 2017-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-10
  • 2016-07-07
相关资源
最近更新 更多