• Qt生成和调用动态库dll,和静态库.a(windows和linux通用)


    系统1:ThinkPad T570、Windows10、QT5.12.2(Qt Creater 4.8.2)
    一、动态库.dll的创建和调用
    1.在qtcreater中按如下步骤创建动态库,动态库名为mydll:

    选择Library项目,C++库在这里插入图片描述
    选择共享库:在这里插入图片描述
    选择qt自带的kit:
    在这里插入图片描述
    在工程中自动生成的mydll.pro文件里内容如下:

    #-------------------------------------------------
    #
     # Project created by QtCreator 2019-04-05T11:14:57
    #
    #-------------------------------------------------
    
    QT       -= gui #在选择需要的模块时,我只选用了QtCore,没有使用QtGui
    
    TARGET = mydll #我配置的动态库的名字:mydll
    TEMPLATE = lib #生成库时该字段为lib;生成执行文件时为:app
    
    DEFINES += MYDLL_LIBRARY #将MYDLL_LIBRARY添加为编译时的预处理器宏,在share_global.h中使用
    
     # The following define makes your compiler emit warnings if you use
     # any feature of Qt which has been marked as 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 you use 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 += 
            mydll.cpp
    
    HEADERS += 
            mydll.h 
            mydll_global.h 
    
    unix {
        target.path = /usr/lib
        INSTALLS += target
    }
    

    mydll.h文件内容如下编写,其中我只添加了sum和squaresum两个函数定义,其他的为自动生成:

    #ifndef MYDLL_H
    #define MYDLL_H
    #include "mydll_global.h"
    class MYDLLSHARED_EXPORT Mydll
    {
    public:
        Mydll();
        int sum(int a,int b);
        int squaresum(int a,int b);
    };
    #endif // MYDLL_H
    

    mydll_global.h文件为自动生成,在此不多描述。

    mydll.cpp文件中如下编写:

    #include "mydll.h"
    Mydll::Mydll()
    {
    }
    int Mydll::sum(int a, int b)
    {
        return (a+b);
    }
    int Mydll::squaresum(int a, int b)
    {
        return (a*a+b*b);
    }

    随后按Ctrl+B键来构建该项目,构建成功后会在工程文件所在的同级目录下生成build-mydll-Desktop_Qt_5_12_2_MinGW_64_bit-Debug文件夹,该文件夹内有libmydll.a,mydll.dll和mydll.o三个文件,我们需要用的是mydll.dll。

    2.在qtcreater中按如下步骤创建使用动态库的工程,工程取名为UseLib。

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述
    将mydll.h和mydll_global.h两个文件从mydll项目文件夹中拷贝到UseLib工程源文件夹下(F:QTCodeTestCodeTestLibuselibUseLib)
    在mainwindow.h文件中添加动态库的头文件#include “mydll.h”,并定义一个动态库类对象Mydll mylib,代码如下:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include "mydll.h"
    namespace Ui {
    class MainWindow;
    }
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
        Mydll mylib;
    private slots:
        void on_pushButton_clicked();
    private:
        Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H

    将mydll项目中生成的mydll.dll文件放到UseLib工程源文件夹下。
    在UseLib.pro文件的最后一行添加如下代码,将动态库包含进当前项目:

    LIBS +=-L$$PWD -lmydll    # $$PWD表示当前路径,mydll根据生成动态库的工程的mydll.pro里面的TARGET = mydll得到
    
    • 1

    mainwindow.ui如下图布局:
    在这里插入图片描述
    mainwindow.cpp如下编写代码:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    void MainWindow::on_pushButton_clicked()
    {
        int a = ui->lineEdit1->text().toInt();
        int b = ui->lineEdit2->text().toInt();
    
        int nsum = mylib.sum(a,b);
        QString str = QString::number(nsum);
        ui->lineEditsum->setText(str);
    }

    运行结果如下:
    在这里插入图片描述

    二、静态库.a的创建和调用
    步骤与动态库几乎相同,除了如下几点:
    1.创建静态库的名字为mylib,选择类型为“静态链接库”,如下:在这里插入图片描述
    2.静态库里面还是一个sum(int a,int b)的方法,静态库创建完之后,在构造出的“build-mylib-Desktop_Qt_5_12_2_MinGW_64_bit-Debugdebug”文件夹下会生成libmylib.a和mylib.o文件,我们要用的是libmylib.a文件。
    3.创建使用静态库的工程,取名为uselib,将libmylib.a和mylib.h拷贝到项目源文件路径下和构造路径下
    4.在uselib.pro中的最后一行添加

    LIBS +=-L$$PWD -lmylib
    
    • 1

    5.在mainwindow.h中添加#include “mylib.h”,并定义一个类对象Mylib mylib;

      #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        #include <QMainWindow>
        #include "mylib.h"
        namespace Ui {
        class MainWindow;
        }
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        public:
            explicit MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
        private slots:
            void on_pushButton_clicked();
            Mylib mylib;
        private:
            Ui::MainWindow *ui;
        };
        #endif // MAINWINDOW_H
    

    6.在mainwindow.cpp中添加如下代码:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    void MainWindow::on_pushButton_clicked()
    {
        int a = ui->lineEdit1->text().toInt();
        int b = ui->lineEdit2->text().toInt();
        int nsum = mylib.sum(a,b);
        QString str = QString::number(nsum);
        ui->lineEditsum->setText(str);
    }

    运行结果如下:

    在这里插入图片描述

    注:linux下按照如上流程创建和调用静态库 亲测成功过,用的系统为
    硬件:NVIDIA Tegra X2
    系统:Ubuntu 16.04LTS
    QT版本:QT5.5.1(Qt Creater 3.5.1)
    但在linux下调用动态库的时候会失败,具体后续我会再研究一下。

  • 相关阅读:
    BroadcastReceiver总结
    一二三(The Seventh Hunan Collegiate Programming Contest)
    使用 JQueryMobile 点击超链接提示“error loading page” 错误
    盒子游戏(The Seventh Hunan Collegiate Programming Contest)
    遗留系统升级改造方案思路
    根据外接鼠标控制笔记本触摸板禁用或启用
    设计模式之策略模式
    android4.3环境搭建
    清晰明亮的白色lua协程(coroutine)
    基于JUnit和Ant测试程序正在运行使用Kieker(AspectJ)监测方法
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14334413.html
Copyright © 2020-2023  润新知