#include "interfacedll.h"
InterfaceDll::InterfaceDll(){}
InterfaceDll::~InterfaceDll(){}
int InterfaceDll::add(int a,int b){ return a+b; }
方法一:右擊“項目”—>屬性—>鏈接器—>常規—>附加庫目錄
輸入dll文件所在的目錄
然後在“輸入”—>附加依賴項,輸入與dll對應的lib文件
(VS2010有時會出現錯誤)
方法二:#pragma comment(lib , "lib文件的路徑名")
#ifndef PLUGINDLL_H
#define PLUGINDLL_H
#include <QtPlugin>
#include "interfacedll.h"
#include <qglobal.h>
#ifdef PLUGINDLL_LIB
# define PLUGINDLL_EXPORT Q_DECL_EXPORT
#else
# define PLUGINDLL_EXPORT Q_DECL_IMPORT
#endif
class PLUGINDLL_EXPORT Plugindll:public QObject,public InterfaceDll
{
Q_OBJECT
Q_INTERFACES(InterfaceDll)
public:
Plugindll();
~Plugindll();
int subInt(int a,int b);
int muitiplyInt(int a,int b);
double devideInt(int a,int b);
private:
};
#endif // PLUGINDLL_H
#include "plugindll.h"
Plugindll::Plugindll(){}
Plugindll::~Plugindll(){}
int Plugindll::subInt(int a,int b) { return a-b;}
int Plugindll::muitiplyInt(int a,int b) { return a*b;}
double Plugindll::devideInt(int a,int b){ return a/(b+0.0);}
Q_EXPORT_PLUGIN2(plugindll,Plugindll)
#pragma comment(lib , "../Win32/debug/InterfaceDll.lib")//引入動態鏈接庫
#include <QtCore/QCoreApplication>
#include "interfacedll.h"//需要使用該頭文件才能調用相應的頭文件
#include <QPluginLoader>
#include <QObject>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QPluginLoader loader("../Win32/debug/Plugindll.dll");
QObject *object=loader.instance();
InterfaceDll *interface=qobject_cast<InterfaceDll *>(object);
if (interface)
{
cout<<interface->add(10,20)<<endl;
cout<<interface->subInt(10,20)<<endl;
cout<<interface->muitiplyInt(10,20)<<endl;
cout<<interface->devideInt(10,20)<<endl;
}
return a.exec();
}