当前位置: 代码迷 >> 综合 >> qt在Windows下调用动态库
  详细解决方案

qt在Windows下调用动态库

热度:91   发布时间:2024-01-15 04:07:32.0
原来调用动态库采用先定义函数

typedefint(*funCD109_Init)(int);

再加载库的方式

    QLibrary mylib1("TSCLib.dll");
    //判断是否正确加载
    if(mylib1.load())
    {
  
        //调用外部函数 add()
         if(tscclearbuffer == NULL)
        {
  
             return false;
        }
    }
今天用另一种方式调用qt生成的动态库:动态库内调用动态库,直接可以使用函数,更方便些。

1、将cclib.dll,cclib_global.h和 iclib.h拷贝到程序目录

2、新建立个qt应用,加入存在的文件cclib.h,cclib_global.h。

3、修改ccdemo.pro,增加LIBS+=-L$$PWD -lcclib

4、在mainwindow.cpp增加头文件,

    Cclib ttt;
    QString ls_str=ttt.read();
    ui->label->setText(ls_str);

5\将cc_32.dll拷贝到debug目录(程序运行目录)

ccdemo.pro:

#-------------------------------------------------
#
# Project created by QtCreator 2016-05-26T08:57:42
#
#-------------------------------------------------QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = ccdemo
TEMPLATE = appSOURCES += main.cpp\mainwindow.cppHEADERS  += mainwindow.h \cclib_global.h \cclib.hFORMS    += mainwindow.ui
LIBS+=-L$$PWD -lcclib
mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<cclib.h>
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);Cclib ttt;QString ls_str=ttt.read();ui->label->setText(ls_str);
}MainWindow::~MainWindow()
{delete ui;
}



  相关解决方案