QT調(diào)用vs2019生成的c++動態(tài)庫的方法實現(xiàn)
dll庫的創(chuàng)建方法:
VS2019創(chuàng)建c++動態(tài)鏈接庫dll與調(diào)用方法-CSDN博客
加減法示范:
頭文件
// 下列 ifdef 塊是創(chuàng)建使從 DLL 導出更簡單的 // 宏的標準方法。此 DLL 中的所有文件都是用命令行上定義的 DLL3_EXPORTS // 符號編譯的。在使用此 DLL 的 // 任何項目上不應(yīng)定義此符號。這樣,源文件中包含此文件的任何其他項目都會將 // DLL3_API 函數(shù)視為是從 DLL 導入的,而此 DLL 則將用此宏定義的 // 符號視為是被導出的。 #ifdef DLL3_EXPORTS #define DLL3_API __declspec(dllexport) #else #define DLL3_API __declspec(dllimport) #endif // 此類是從 dll 導出的 class DLL3_API CDll3 { public: CDll3(void); int name; int age; // TODO: 在此處添加方法。 }; extern DLL3_API int nDll3; extern DLL3_API CDll3; extern "C" { DLL3_API int fnDll3(void); DLL3_API int fnAdd(int a, int b); DLL3_API int fnSub(int a, int b); }
cpp
// Dll3.cpp : 定義 DLL 的導出函數(shù)。 // #include "pch.h" #include "framework.h" #include "Dll3.h" // 這是導出變量的一個示例 DLL3_API int nDll3=666666; // 這是導出函數(shù)的一個示例。 DLL3_API int fnDll3(void) { return 666; } DLL3_API int fnAdd(int a, int b) { return a + b; } DLL3_API int fnSub(int a, int b) { return a - b; } // 這是已導出類的構(gòu)造函數(shù)。 CDll3::CDll3() { return; }
每次修改后:都執(zhí)行-》重新生成,確保 dll和lib文件的同步更新
記得 選擇release,x64
把頭文件.h與dll,lib放在一個地方以便拷貝到QT項目
QT創(chuàng)建一個項目:
把頭文件.h與dll,lib拷貝到項目文件夾里面
構(gòu)建編譯:
查看默認編譯后的路徑:
拷貝到lib,和dll到exe生成的目錄
QT顯式調(diào)用dll
cpp主文件添加頭文件:
構(gòu)造函數(shù)添加代碼:
dll隱式調(diào)用
pro文件添加:修改Dll3, Dll3表示Dll3.lib
就可以直接調(diào)用了:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QLibrary> #include <iostream> #include <QMessageBox> #include "Dll3.h" #include <QDebug> typedef int ( *pAdd)(int , int); //定義函數(shù)指針 using namespace std; //using namespace MathFunc; //extern "C" __declspec(dllexport) int fnAdd(int a, int b); //extern "C" __declspec(dllexport) int fnAdd(int a, int b); MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); //dll隱式調(diào)用 int cc = fnAdd(2,6); qDebug()<<"cc value is :"<<QString::number(cc); int cc_sub = fnSub(2,6); qDebug()<<"cc_sub value is :"<<QString::number(cc_sub); int n_dll3 = nDll3; qDebug()<<"n_dll3 value is :"<<QString::number(n_dll3); CDll3 aa; aa.age =13; CDll3 bb; bb.age =15; qDebug()<<"aa.age value is :"<<QString::number(aa.age); qDebug()<<"bb.age value is :"<<QString::number(bb.age); // 顯示調(diào)用dll // QLibrary mydll("Dll3.dll"); //與exe相同目錄 // mydll.load(); // if(mydll.isLoaded()) // { // pAdd add = (pAdd)mydll.resolve("fnAdd"); // if(add) // { // int ret = add(1,7); //在 這里調(diào)用DLL里的函數(shù) // QMessageBox::information(this,"value","get_value is: "+QString::number(ret)); // cout<< ret << endl ; // } // add = (pAdd)mydll.resolve("fnSub"); // if(add) // { // int ret = add(9,7); //在 這里調(diào)用DLL里的函數(shù) // QMessageBox::information(this,"value","get_value is: "+QString::number(ret)); // cout<< ret << endl ; // } // mydll.unload(); // } } MainWindow::~MainWindow() { delete ui; }
到此這篇關(guān)于QT調(diào)用vs2019生成的c++動態(tài)庫的方法實現(xiàn)的文章就介紹到這了,更多相關(guān)QT調(diào)用c++動態(tài)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!