qt用dlopen加載so
阿新 • • 發佈:2019-04-13
不能 析構 bte har int RoCE .cpp roo 什麽 源碼:
https://github.com/haidragon/QtloadSo
在linux中使用dlopen是要額外添加庫的,或者在編譯的時候要加-ldl
現在我們在qt中創建一個SoMain(so)項目。
和普通項目一樣,創建一個窗口項目。運行下確保qt程序能正常運行。
源碼:
main.cpp
https://github.com/haidragon/QtloadSo
在linux中使用dlopen是要額外添加庫的,或者在編譯的時候要加-ldl
現在我們在qt中創建一個SoMain(so)項目。
和普通項目一樣,創建一個窗口項目。運行下確保qt程序能正常運行。
源碼:
main.cpp
#include "mainwindow.h" #include <QApplication> #include<QMessageBox> #include<stdio.h> #include<stdlib.h> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
修改pro文件
TEMPLATE = app
修改成
TEMPLATE = lib文件
在main.cpp文件中添加運行構造與析構函數 動態庫main函數是不會運行的
#include "mainwindow.h" #include <QApplication> #include<QMessageBox> #include<stdio.h> #include<stdlib.h> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } void __attribute__ ((constructor)) SoMain(int argc, char *argv[]){ QApplication a(argc, argv); QMessageBox::about(nullptr, "About", "SoMain"); } void __attribute__ ((constructor)) SoMain2(int argc, char *argv[]){ QApplication a(argc, argv); QMessageBox::about(nullptr, "About", "SoMain2"); } void __attribute__ ((destructor)) SoUnload(int argc, char *argv[]){ printf("SoUnload \n"); }
生成so文件
寫個main函數加載它
//#include "mainwindow.h" //#include <QApplication> #include <stdio.h> #include <dlfcn.h> int main(int argc, char *argv[]) { void * libm_handle = NULL; libm_handle = dlopen("/root/Desktop/build-test-Desktop_Qt_5_12_0_GCC_64bit-Debug/libtest.so", RTLD_LAZY ); // QApplication a(argc, argv); // MainWindow w; // w.show(); // return a.exec(); }
會依次運行構造與析構函數
構造函數1
構造函數2
析構函數1 這裏不能像構造函數一樣彈框 應該是沒了qt對象了 可以看看qt源碼它什麽時候釋放的QApplication。
現在我們用qt項目加載so文件
新創建一個項目
代碼
#include "mainwindow.h"
#include <QApplication>
#include <stdio.h>
#include <dlfcn.h>
int main(int argc, char *argv[])
{
void * libm_handle = NULL;
libm_handle = dlopen("/root/Desktop/build-test-Desktop_Qt_5_12_0_GCC_64bit-Debug/libtest.so", RTLD_LAZY );
// QApplication a(argc, argv);
// MainWindow w;
// w.show();
// return a.exec();
}
運行時會報錯找不到符號
在pro文件中加上:
LIBS += -L/lib/x86_64-linux-gnu -ldl
也可以用qt自己封裝的加載庫函數,但是不太好控制。
qt用dlopen加載so