2016-07-27 48 views
1

我有一個QT應用程序app.exe和一個QT插件plugin.dll。我的plugin.dll取決於許多其他動態庫(例如lib1.dlllib2.dll等)。要分發項目中,我有這樣的文件夾結構(忽略Qt庫):用自己的DLL部署QT插件

app.exe 
plugins\ 
    plugin.dll 
lib1.dll 
lib2.dll 
lib3.dll 

的問題是,有上libX.dll太多的依賴性,而我希望把他們藏在一個插件文件夾,例如:

app.exe 
plugin\ 
    plugin.dll 
    lib1.dll 
    lib2.dll 
    lib3.dll 

但是這種方式庫libX.dll是「看不見」我的插件,所以它不能被加載。有什麼辦法可以解決這個問題嗎?

我使用這個代碼在plugin.dllpro -file導入libX.dll

LIBS += -Lpath -l lib1 -l lib2 -l lib3 

回答

0

一個解決這個問題的方法之一是:

  1. 動態鏈接庫的所有(在運行時)
  2. 添加額外位置以搜索庫

這些變化應在plugin.dll代碼來完成:

/* Declare a pointer to import function */ 

typedef void (*FUNCTION)(); 
FUNCTION f; 

/* Make system search the DLLs in my plugin folder */ 

// Variable "app" contains directory of the application, not the plugin 
QDir app = QDir(qApp->applicationDirPath()); 
// Combine path 
QString plugin_path = app.filePath("plugins/"); 
// Adding full path for DLL search 
SetDllDirectory(plugin_path.toStdWString().c_str()); 

/* Linking the library */ 

QLibrary mylib("mylib.dll"); 
f = (FUNCTION) mylib.resolve("function"); 
if (f != NULL) 
    f(); // You got the function from DLL 
else 
    return; // DLL could not be loaded 

該解決方案具有短處:

  • 它不是獨立於平臺的(我認爲你能避免在類UNIX系統中使用SetDllDirectory但我不知道)
  • 如果導入了大量的功能,你將有很多的指針

是否有人知道純粹的Qt解決方案?