大家好
我的项目使用 MinGW、QT 和 CMake。
如图所示,我的项目有两个模块。
libRinzoCore.DLL - 一个共享库,它定义了一些抽象类和接口(interface)以及应用程序的一些核心功能。该模块用于实现动态插件(它们也是由应用程序自动加载的共享库)。
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”时它崩溃并显示消息(这是从日语错误消息翻译过来的)
" Application cannot performe correctly (0xc0000005). Click [OK] to cancel "
这是我的 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 上运行良好。
有什么建议吗?
最佳答案
在 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()
{
...
}
关于C++ MinGW 共享库问题(仅限 Windows,适用于 Linux)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3606043/