c++ - 使用 QLibrary 加载库

标签 c++ qt shared-libraries qlibrary

我发现了以下用于在 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;

最佳答案

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);

关于c++ - 使用 QLibrary 加载库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45119131/

相关文章:

c++ - pic vs vs pie之间的区别

linux - Dependency Walker 等同于 Linux?

c++ - 从共享对象中的类继承

c++ - 堆上的内存地址是否被重用?

c++ - Objective-C和C++混合编译错误 “_OBJC_CLASS_$_UIResponder”

c++ - Qt/OpenGL : Am I using PBO correctly ?

c++ - 在文本浏览器中打印 Qt 浮点变量

c++ - 使用 regex_* 解析/etc/passwd,非标准行为 C++

c++ - 使用 Global with Value 时的 V8 段错误

c++ - C++/Qt 中的闭合轮廓