首先定義頭文件
#ifndef HELLOQTDLL_H
#define HELLOQTDLL_H
//下面這段代碼不能少,MY_EXPORT可以更改
#ifdef Q_WS_WIN
#define MY_EXPORT __declspec(dllexport)
#else
#define MY_EXPORT
#endif
#include <QtGui/QMainWindow>
class HelloQtDll : public QMainWindow
{
Q_OBJECT
public:
HelloQtDll(QWidget *parent = 0);
~HelloQtDll();
};
// 如果是C++編譯的函數, 要使用extern "C"來包裝成C函數(導出函數, 給外部提供服務).
#endif // HELLOQTDLL_H
源文件
#include "helloqtdll.h"
#include <QMessageBox>
HelloQtDll::HelloQtDll(QWidget *parent)
: QMainWindow(parent)
{
QMessageBox::information(this,tr("Qt Dll test"),tr("Constructor!"));
}
HelloQtDll::~HelloQtDll()
{
}
extern "C" MY_EXPORT int add(int a, int b)//定義外部調用的接口 add
{
return (a + b) / ;//方法內部可以調用其它函數
}