testDll和testExe项目
备注:windows下dll内不需要new QApplication, linux和mac下面需要在动态库里面new QApplication
testdll.h
1 #ifndef TESTDLL_H 2 #define TESTDLL_H 3 4 #include"testDll_global.h" 5 6 extern "C" TESTDLLSHARED_EXPORT void initApp(void); 7 extern "C" TESTDLLSHARED_EXPORT void showDlg(void); 8 9 extern "C" void cleanApp(void); 10 11 class QApplication; 12 static QApplication* shareApplication; 13 14 #endif//TESTDLL_H
testDll_global.h
1 #ifndef TESTDLL_GLOBAL_H 2 #define TESTDLL_GLOBAL_H 3 4 #include <QtCore/qglobal.h> 5 6 #if defined(TESTDLL_LIBRARY) 7 # define TESTDLLSHARED_EXPORT Q_DECL_EXPORT 8 #else 9 # define TESTDLLSHARED_EXPORT Q_DECL_IMPORT 10 #endif 11 12 #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=0; //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",0); // shareApplication->quit(); // shareApplication=NULL; } }
testExe的测试cpp里面
1 void MainWindow::on_pushButton_clicked() 2 { 3 QLibrary dll("testDll"); 4 5 if( !dll.load() ) return; 6 7 typedef void(*DLL_INIT_APP)(); 8 typedef void(*DLL_SHOW_DLG)(); 9 10 DLL_INIT_APP dll_initApp = (DLL_INIT_APP)dll.resolve("initApp"); 11 12 DLL_SHOW_DLG dll_showDlg= (DLL_SHOW_DLG)dll.resolve("showDlg"); 13 14 dll_initApp(); 15 dll_showDlg(); 16 }