使用 qt 创建和使用共享库 [英] create and use shared library with qt

查看:73
本文介绍了使用 qt 创建和使用共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是共享库的新手,所以我对如何创建/使用共享库有疑问,我正在使用 Qt Creator 和 qt 5.4.2 和 Microsoft Visual C++ 11.0 Compliler.

I am new to the shared library stuff, so I have question about how to create/use a shared library, I am using Qt Creator with qt 5.4.2 with Microsoft Visual C++ 11.0 Compliler.

在我的项目中,我需要创建一个从外部库调用函数的 dll(有 .h、.lib、.dll 可供使用).为了了解从库中导出/导入函数的工作原理,我尝试创建一个带有一个函数的简单库,然后首先在另一个程序中使用它.在阅读了不同的教程后,我设法创建了这个库.在 Qt Creator 中,New Project->Library(C++ Library)->Type(shared library)Name:sharedlib->Modules(QtCore)->Finish.

In my project, I will need to create a dll which call functions from an external library (there are .h, .lib, .dll to use from). To understand how export/import of functions from library work, I tried to create a simple library with one function and use this in another programm first. After reading different tutorials, I managed to create the library. In Qt Creator, New Project->Library(C++ Library)->Type(shared library)Name: sharedlib->Modules(QtCore)->Finish.

sharedlib.h:

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

sharedlib.cpp:

sharedlib.cpp:

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

extern "C" SHAREDLIBSHARED_EXPORT int add(int a, int b)
{
    return a + b;
}

只添加了一个简单的加2个数字的函数.

only added a simple function to add 2 numbers.

构建后,我得到 sharedlib.dllsharedlib.lib 和其他一些文件,(没有像某些教程中的 .a 文件,我认为这是因为我是使用提供 .lib 文件的 microsoft vc 编译器).

after build, I get sharedlib.dll and sharedlib.lib and some other files, (no .a file like in some tutorials, I thought its because I am using microsoft vc compiler which give the .lib file instead).

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

Now to create a second programm in which I want to use the library: New Project->Qt Console Application->Name(loadlib)->Finish, then I copied the sharedlib.lib, sharedlib.h, sharedlib.dll into the loadlib directory. (do I need them all? and where shall I put them exactly?) According to tutorial, right-click on the project->add library->external library->choose the .lib file inside the loadlib directory, uncheck the Linux and Mac under Platform and choose the Dynamic Linkage. this is my 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/

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

  1. if I put .h and .dll/.lib file in subfolder like loadlib/include and loadlib/libs, it will change to INCLUDEPATH += $$PWD/include DEPENDPATH += $$PWD/include and LIBS += -L$$PWD/libs -lsharedlib, right?
  2. do I need to copy all 3 files to my loadlib directory?
  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();
}

我实际上如何在这里使用 add 函数?

how do I actually use the add function here?

EDIT:我改变了一些东西,去掉了sharedlib_global.h并将内容粘贴到sharedlib.h中,去掉了类,我可以直接调用一个函数而不用把它包装成一个班?

EDIT: I changed few things, got rid of the sharedlib_global.h and paste the content into sharedlib.h, and get rid of the class,can I call a function directly without wrap this into a class?

推荐答案

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

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

部署时需要将sharedlib.dll和可执行文件打包在同一目录下.

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

这篇关于使用 qt 创建和使用共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆