2010-08-31 73 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