Qt调用qt生成的dll 显式和隐式(静态动态)
2016-05-27 17:01
561 查看
昨天突然想到 :接触了这么久的qt了 dll(动态链接库)还没有用过就打算事件一下 差了半天资料 感觉网上没有一片文章讲述的全面详细,于是就有个这篇博客。环境:win7+qt5.3.2开始:一、qt生成dll1.建立生成dll的工程,选择C++库然后就是写动态库的函数。2.在这里我要做个详细的解释:如果你要隐式的调用dll的话,就直接把函数写成q的类成员函数,如果要是显式调用的话 一定要声明称C的方式(接下来会详细谈到,因为我在调用的时候就遇到了这样的问题)。3.先介绍隐式调用// =======sld.h=====================
#ifndef SLD_H
#define SLD_H
#include "sld_global.h"
class SLDSHARED_EXPORT Sld
{
public:
Sld();
QString GetStr();
};
#endif // SLD_H//===============sld.cpp================
#include "sld.h"
#include <QString>
Sld::Sld()
{
}
QString Sld::GetStr()
{
return "hello world";
}这就是dll的工程,动态库只有一个函数,返回“hello world”;编译之后 就会在debug文件里有一个sld.dll文件 这个就是生成的动态库 .a文件也在里面。新建一个工程来调用dll (在界面上添加一个button用来调用动态库的函数)//===================.pro=====================
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = testDll
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
LIBS +=-L D:/QtProjects/testDll/dll/ -lsld #动态库头文件以及动态库的位置// ===================.h=======================
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};//==================.cpp=============
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "./dll/sld.h"
#include <QMessageBox>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
Sld sld;
qDebug()<<GetStr();
}Notice: 最关键的一点:一定要把生成的dl复制一个l放在跟exe同文件夹下,要不然会直接运行报错。// 结果这个就是隐式调用。二、显式调用1.动态库的创建跟上面的差不多,唯独不一样的地方是在函数声明的时候要用extern “C”声明//+===============sld.h================
#ifndef SLD_H
#define SLD_H
#include "sld_global.h"
class SLDSHARED_EXPORT Sld
{
public:
Sld();
};
extern "C" SLDSHARED_EXPORT QString GetStr();
//=============sld.cpp=====================
1.新建一个工程。//===========pro==============#define DLL __declspec(dllexport)#include "sld.h"#include <QString>Sld::Sld(){}QString GetStr(){return "hello world";}
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = testDll
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
LIBS +=-L D:/QtProjects/testDll/dll/ -lsld #动态库头文件以及动态库的位置在main函数里写上//=========main.cpp=================
#include "mainwindow.h"
#include <QApplication>
#include "./dll/sld.h"
#include <QLibrary>
#include <QDebug>
typedef QString (*funtest)(); // 函数指针
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// MainWindow w;
// w.show();
QLibrary mylib("sld.dll");
if(mylib.load())
qDebug()<<"load ok!";
funtest hello = (funtest)mylib.resolve("GetStr");
if(hello)
{
qDebug()<<"load hello ok!";
qDebug()<<hello();
}
else
{
qDebug()<<"load GetStr failed!";
}
return a.exec();
}
结果:有什么不明白 或者内容有误的话 可以聊 Q:745121731
相关文章推荐
- QT学习 第一章:基本对话框
- 使用Shiboken为C++和Qt库创建Python绑定
- Qt 5.6更新至RC版,最终版本近在咫尺
- C#隐式运行CMD命令(隐藏命令窗口)
- C#数值转换-隐式数值转换表参考
- JavaScript隐式类型转换
- 简单介绍JavaScript数据类型之隐式类型转换
- Qt定时器和随机数详解
- Java基础之隐式转换vs强制转换
- 编程之显示/隐式声明
- Qt实现图片移动实例(图文教程)
- C#数值转换-显式数值转换表(参考)
- Qt for Android开发实例教程
- QModelIndex/Role/Model介紹<二>
- Qt Model/View/Delegate浅谈 - QAbstractListModel
- Qt Model/View/Delegate浅谈 - roleNames()
- 基于PyQt5的快速开发框架QFramer
- OSX下安装PyQt
- ok6410开发板移植DirectFB手记
- 【笔记】给Qt内嵌一个Chrome吧