QT动态库及静态库的编写的使用

1.动态库的编写

        1.1 创建动态库

 

 然后一直下一步就建立好了

我建立的目录如下

 TestLib.pro

QT -= gui

TARGET = TestLib #指定生成的应用程序名
TEMPLATE = lib #模板变量告诉qmake为这个应用程序生成哪种makefile
DEFINES += TESTLIB_LIBRARY #应用程序所需的额外的宏定义列表

CONFIG += c++11 #用来告诉 qmake 关于应用程序的配置信息

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \ #生成的.cpp文件
    testlib.cpp

HEADERS += \ #生成的.h文件
    TestLib_global.h \
    testlib.h

# Default rules for deployment. #部署的默认规则
unix {
    target.path = /usr/lib
}
!isEmpty(target.path): INSTALLS += target

 testlib.h

#ifndef TESTLIB_H
#define TESTLIB_H

#include "TestLib_global.h"

class TESTLIB_EXPORT TestLib
{
public:
    TestLib();
    // 自定库函数
    int MyAdd();
};

#endif // TESTLIB_H

TestLib_global.h

#ifndef TESTLIB_GLOBAL_H
#define TESTLIB_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined(TESTLIB_LIBRARY)
#  define TESTLIB_EXPORT Q_DECL_EXPORT
#else
#  define TESTLIB_EXPORT Q_DECL_IMPORT
#endif

#endif // TESTLIB_GLOBAL_H

testlib.cpp

#include "testlib.h"

TestLib::TestLib()
{
}

int TestLib::MyAdd()
{
    return (1 + 2);
}

        1.2 创建静态库

步骤和上面基本一致只有在选库类型的时候选择静态库

 创建出来的目录如下

TestStaticLib.pro

QT -= gui

TARGET = TestStaticLib
TEMPLATE = lib
CONFIG += staticlib

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    teststaticlib.cpp

HEADERS += \
    teststaticlib.h

# Default rules for deployment.
unix {
    target.path = $$[QT_INSTALL_PLUGINS]/generic
}
!isEmpty(target.path): INSTALLS += target

 teststaticlib.h

#ifndef TESTSTATICLIB_H
#define TESTSTATICLIB_H

class TestStaticLib
{
public:
    TestStaticLib();
    int MyStaticLib();
};

#endif // TESTSTATICLIB_H

teststaticlib.cpp

#include "teststaticlib.h"

TestStaticLib::TestStaticLib()
{
}

int TestStaticLib::MyStaticLib()
{
    return 3 + 4;
}

完成以上两项之后分别对其进行构建

3.建立自己的项目去调用库

我建立的项目如下

UseMyLib.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    usermylib.cpp
HEADERS += \
    usermylib.h


#根据实际路径添加                       库文件所在文件夹                            生成的库文件libTestLib.a(只写TestLib)
LIBS += -LD:\Qtpro\build-TestLib-Desktop_Qt_5_14_2_MinGW_64_bit-Debug\debug -lTestLib \
    -LD:\Qtpro\build-TestStaticLib-Desktop_Qt_5_14_2_MinGW_64_bit-Debug\debug -lTestStaticLib
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

usermylib.cpp

#include "usermylib.h"
#include <QDebug>
#include "../TestLib/testlib.h"
#include "../TestStaticLib/teststaticlib.h"
UserMyLib::UserMyLib(QWidget *parent)
    : QMainWindow(parent)
{
    TestLib lib;


    qDebug() << lib.MyAdd();

    TestStaticLib StaticLib;
    qDebug() << StaticLib.MyStaticLib();

}

UserMyLib::~UserMyLib()
{
}

其他文件未做修改

然后编译运行看效果就好了

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值