2010-08-31 29 views
2

全部問候,C++ MinGW共享庫問題(僅限Windows,適用於Linux)?

我爲我的項目使用MinGW,QT和CMake。

http://i34.tinypic.com/30w85xt.png

如圖中所示,我的項目有兩個模塊。

  1. libRinzoCore.DLL - 共享庫,其定義了一些抽象類和接口和application.This模塊的某些核心功能用於實現動態插件(也共享其由應用程序自動加載的庫) 。

  2. Rinzo.exe - 主要應用程序。它使用「libRinzoCore」類。

「libRinzoCore」主要是使用QT對象開發的,並與QT庫鏈接。

「Rinzo.exe」也使用QT庫對象,有些未在「libRinzoCore」中使用。所以我必須將QT庫和「libRinzoCore」鏈接到該可執行文件。

我可以編譯「libRinzoCore」沒有問題,而且它生成的兩個文件「libRinzoCore.DLL」和「libRinzoCore.DLL.a」

但在編制「Rinzo.exe」它提供了以下的輸出:

Linking CXX executable Rinzo.exe 
Info: resolving IRzPlugin::staticMetaObject  by linking to __imp___ZN9IRzPlugin16staticMetaObjectE (auto-import) 
Info: resolving IRzViewerPlugin::staticMetaObject  by linking to __imp___ZN15IRzViewerPlugin16staticMetaObjectE (auto-import) 
Info: resolving IRzLayeringPlugin::staticMetaObject  by linking to __imp___ZN17IRzLayeringPlugin16staticMetaObjectE (auto-import) 
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: warning: auto-importing has been activated without --enable-auto-import specified on the command line. 
This should work unless it involves constant data structures referencing symbols 
from auto-imported DLLs. 
[100%] Built target Rinzo 

和執行時「Rinzo.exe」它與消息(這是來自日本的錯誤消息翻譯)

「應用程序不能performe 正確地崩潰(0000005)。點擊[確定] 取消「

這裏是我的CMake文件

libRinzoCore:

http://www.keepandshare.com/doc/2199086/rinzocore-txt-august-31-2010-12-10-pm-2k?da=y

Rinzo.exe:

http://www.keepandshare.com/doc/2199085/rinzo-txt-august-31-2010-12-10-pm-5k?da=y

它的工作原理好,如果我編譯「libRinzoCore」爲一個靜態庫。 在Linux上運行良好。

任何提示?

回答

2

在Windows上,您需要聲明「導出」動態庫的一部分才能使其工作。

#ifdef Q_WS_WIN 
#ifdef RINZO_EXPORT 
#define RINZO_LIB __declspec(dllexport) 
#else 
#define RINZO_LIB __declspec(dllimport) 
#endif 
#else 
#define RINZO_LIB 
#endif 

然後,你需要把RINZO_LIB在你的類聲明的前LIB內(只類新要「出口」,在外部代碼中使用)

class RINZO_LIB YourExportedClass 
{ 
... 
} 

最後一部分是添加預處理器宏,同時編譯你的庫。正如你可以看到它是RINZO_EXPORT

請記住,不要在「導入」(使用庫之外的代碼)時添加此預處理器宏。

而且所有功能需要RINZO_LIB宏觀可見外庫:

RINZO_LIB void yourExportedFunction() 
{ 
... 
} 
+0

非常感謝Kamil.You救了我的一天!!!! – 2010-08-31 09:19:06

+0

Qt還提供了一組簡化此步驟的宏。您可以在Qt文檔的[Creating Shared Libraries](創建共享庫)(http://doc.qt.io/qt-5/sharedlibrary.html)一章中找到這些建議。 – SGaist 2017-01-17 15:15:02