用qt创建和使用共享库

yan*_*nny 5 qt shared-libraries

我是共享库的新手,所以我对如何创建/使用共享库有疑问,我将Qt Creator与带有Microsoft Visual C ++ 11.0 Compliler的qt 5.4.2一起使用。

在我的项目中,我将需要创建一个dll,以从外部库(有.h,.lib和.dll使用)中调用函数。为了了解如何从库中导出/导入函数,我尝试创建一个具有一个函数的简单库,并首先在另一个程序中使用它。在阅读了不同的教程之后,我设法创建了该库。在Qt Creator中,新建项目->库(C ++库)->类型(共享库)名称:sharedlib-> Modules(QtCore)-> Finish。

sharedlib.h:

#ifndef SHAREDLIB_H
#define SHAREDLIB_H

#include <QtCore/qglobal.h>

#if defined(SHAREDLIB_LIBRARY)
#  define SHAREDLIBSHARED_EXPORT Q_DECL_EXPORT
#else
#  define SHAREDLIBSHARED_EXPORT Q_DECL_IMPORT
#endif

extern "C" SHAREDLIBSHARED_EXPORT int add(int a, int b);

#endif // SHAREDLIB_H
Run Code Online (Sandbox Code Playgroud)

sharedlib.cpp:

#include "sharedlib.h"
#include <stdio.h>

extern "C" SHAREDLIBSHARED_EXPORT int add(int a, int b)
{
    return a + b;
}
Run Code Online (Sandbox Code Playgroud)

仅添加了一个简单的函数来添加2个数字。

生成后,我得到sharedlib.dllsharedlib.lib和其他文件(没有像某些教程中那样的.a文件,我认为是因为我使用的是Microsoft vc编译器,而不是.lib文件)。

现在创建第二个我要在其中使用该库的程序:New Project-> Qt Console Application-> Name(loadlib)-> Finish,然后将其复制sharedlib.lib, sharedlib.h, sharedlib.dll到loadlib目录中。(我需要它们吗?我应该把它们放到哪里吗?)根据教程,右键单击项目->添加库->外部库->选择loadlib目录中的.lib文件,取消选中Linux和Mac在“平台”下,然后选择“动态链接”。这是我的loadlib.pro:

QT       += core
QT       -= gui

TARGET = loadlib
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app
SOURCES += main.cpp

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/ -lsharedlib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/ -lsharedlib

INCLUDEPATH += $$PWD/
DEPENDPATH += $$PWD/
Run Code Online (Sandbox Code Playgroud)
  1. 如果我将.h和.dll / .lib文件放在类似loadlib / include和loadlib / libs的子文件夹中,它将更改为INCLUDEPATH += $$PWD/include DEPENDPATH += $$PWD/includeLIBS += -L$$PWD/libs -lsharedlib,对吗?
  2. 是否需要将所有3个文件复制到我的loadlib目录?
  3. main.cpp:

    #include <QCoreApplication>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        // simple Debug output to add 7 and 3
    
        return a.exec();
    }
    
    Run Code Online (Sandbox Code Playgroud)

    我实际上如何在这里使用添加功能?

编辑:我做了几件事,摆脱了sharedlib_global.h并将内容粘贴到sharedlib.h中,并摆脱了类,我可以直接调用函数而不将其包装到类中吗?

小智 5

到目前为止,您所做的一切都是正确的。现在,只需将库头文件sharedlib.h包含在main.cpp或任何文件中,然后就可以在其中使用add()函数。

#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();
}
Run Code Online (Sandbox Code Playgroud)

部署时,您需要将sharedlib.dll与可执行文件一起打包在同一目录中。


ram*_*ror 2

试试这个(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.
}
Run Code Online (Sandbox Code Playgroud)

如果您可以编译上述内容,那么您的库就可以按预期工作。