testDll和testExe项目
备注:windows下dll内不需要new QApplication, linux和mac下面需要在动态库里面new QApplication
testdll.h
#ifndef TESTDLL_H
#define TESTDLL_H #include"testDll_global.h" extern "C" TESTDLLSHARED_EXPORT void initApp(void);
extern "C" TESTDLLSHARED_EXPORT void showDlg(void); extern "C" void cleanApp(void); class QApplication;
static QApplication* shareApplication; #endif//TESTDLL_H
testDll_global.h
#ifndef TESTDLL_GLOBAL_H
#define TESTDLL_GLOBAL_H #include <QtCore/qglobal.h> #if defined(TESTDLL_LIBRARY)
# define TESTDLLSHARED_EXPORT Q_DECL_EXPORT
#else
# define TESTDLLSHARED_EXPORT Q_DECL_IMPORT
#endif #endif // TESTDLL_GLOBAL_H
testdll.cpp
#include<QApplication>
#include<QMessageBox>
#include"TestDll.h"
#include"Form.h" extern "C" TESTDLLSHARED_EXPORT void initApp(void)
{
shareApplication=NULL;
} extern "C" TESTDLLSHARED_EXPORT void showDlg(void)
{
if( NULL == shareApplication )
{
int argc=; //windows下注释掉
// shareApplication = new QApplication(argc,NULL); Form* pForm=new Form;
pForm->show(); // shareApplication->exec();
}
} extern "C" void cleanApp(void)
{
if( NULL != shareApplication )
{
QMessageBox::information(NULL,"","appdestroyed",); // shareApplication->quit();
// shareApplication=NULL;
}
}
testExe的测试cpp里面
void MainWindow::on_pushButton_clicked()
{
QLibrary dll("testDll"); if( !dll.load() ) return; typedef void(*DLL_INIT_APP)();
typedef void(*DLL_SHOW_DLG)(); DLL_INIT_APP dll_initApp = (DLL_INIT_APP)dll.resolve("initApp"); DLL_SHOW_DLG dll_showDlg= (DLL_SHOW_DLG)dll.resolve("showDlg"); dll_initApp();
dll_showDlg();
}