create and use shared library with qt

僤鯓⒐⒋嵵緔 提交于 2019-12-05 08:26:12

Everything you've done so far is correct. Now just include the library header file sharedlib.h in your main.cpp or whichever file and you can then use add() function there.

#include <QCoreApplication>
#include <QDebug>
#include "sharedlib.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    // simple Debug output to add 7 and 3

    SharedLib lib;
    int a = 5, b = 6;
    int sum = lib.add (a, b);

    return a.exec();
}

You need to pack sharedlib.dll in the same directory along with the executable file when deploying.

Try this (main.cpp):

#include "sharedlib.h"

#include <QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    // simple Debug output to add 7 and 3
    SharedLib sLib;
    qDebug() << sLib.add(7, 3); // should print 10

    return 0;   // just exit

//    return a.exec();  // you need to kill / force stop your app if you do ths.
}

If you can compile the above, then your library is working as expected.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!