【问题标题】:Qt/mingw32 undefined reference errors... unable to link a .libQt/mingw32 undefined reference errors...无法链接.lib
【发布时间】:2009-07-16 12:45:19
【问题描述】:

我是 Qt 新手,遇到一个无法修复的错误。

我有一堆 Windows (VS2005) 静态库文件 (.lib)。我正在测试它们是否可以很好地与 Qt 配合使用。所以我选择了我拥有的最简单的库。 (叫MessageBuffer)。

所以我将MessageBuffer.h添加到main.cpp中,并将这些文件的位置添加到.proINCLUDEPATH中。

在那之前一切看起来都很好,我可以使用类和 Qt IDE 显示所有方法和所有内容。所以对我来说,它看起来像是找到了 .h 文件。

现在我在.pro 中添加了MessageBuffer.lib(VS2005/Debug 版本),如下所示:

LIBS += E:/SharedLibrary/lib/MessageBufferd.lib

我还尝试了以下方法:

win32:LIBS += E:/SharedLibrary/lib/MessageBufferd.lib
LIBS += -LE:/SharedLibrary/lib -lMessageBufferd
win32:LIBS += -LE:/SharedLibrary/lib -lMessageBufferd

这是我的.pro文件的内容:

QT += opengl
TARGET = SilverEye
TEMPLATE = app
INCLUDEPATH += E:/SharedLibrary/MessageBuffer
SOURCES += main.cpp \
    silvereye.cpp
HEADERS += silvereye.h
FORMS += silvereye.ui
OTHER_FILES += 
win32:LIBS += E:/SharedLibrary/lib/MessageBufferd.lib

他们都给我同样的错误:(即使我不包括.lib,我也会得到同样的错误)

Running build steps for project SilverEye...
Configuration unchanged, skipping QMake step.
Starting: C:/Qt/2009.03/mingw/bin/mingw32-make.exe -w 
mingw32-make: Entering directory `C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye'
C:/Qt/2009.03/mingw/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye'
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -mthreads -Wl -Wl,-subsystem,windows -o debug\SilverEye.exe debug/main.o debug/silvereye.o debug/moc_silvereye.o -L"c:\Qt\2009.03\qt\lib" -lopengl32 -lglu32 -lgdi32 -luser32 -lmingw32 -lqtmaind E:/SharedLibrary/lib/MessageBufferd.lib -lQtOpenGLd4 -lQtGuid4 -lQtCored4
mingw32-make[1]: Leaving directory `C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye'
mingw32-make: Leaving directory `C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye'
debug/main.o: In function `Z5qMainiPPc':
C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye/main.cpp:12: undefined reference to `MessageBuffer::MessageBuffer()'
C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye/main.cpp:13: undefined reference to `MessageBuffer::Append(char*, int)'
C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye/main.cpp:17: undefined reference to `MessageBuffer::~MessageBuffer()'
C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye/main.cpp:17: undefined reference to `MessageBuffer::~MessageBuffer()'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\SilverEye.exe] Error 1
mingw32-make: *** [debug] Error 2
Exited with code 2.
Error while building project SilverEye
When executing build step 'Make'

有人可以帮忙吗?

【问题讨论】:

  • 你能把你正在使用的专业文件贴出来吗?
  • 只是在原消息中添加了pro文件内容。

标签: c++ qt linker mingw


【解决方案1】:

根据问题 Use libraries compiled with visual studio in an application compiled by g++ (mingw) 和 MSDN 论坛帖子 I can't mix VC & GCC,您似乎无法将 gcc 应用程序与可视化 c++ 编译库链接。

解决方案是使用相同的编译器重新编译所有内容。

【讨论】:

  • 这实际上是我要发布的内容,我发现:这些库与许多其他预编译的 Windows 开发包(如 MySQL 客户端库)一样,是使用 Ms Visual C++ (MSVC) 编译的。我们发现,MSVC 和 MinGW 对 stdcall 函数使用不同的命名约定。 MSVC 将它们导出为 _name@ordinal,但 MinGW 将它们导出为名称。因此,在调用从 MSVC 库导出的 stdcall 函数时,MinGW 构建失败并出现“未定义的引用”链接错误。
  • 请在此处查找解决方案:blog.outofhanwell.com/2006/05/01/…
【解决方案2】:

MinGW FAQ 讨论了这个问题并提供了解决方案:

  1. 使用 reimp(用于 lib 文件)或 pexports(用于 dll 文件)创建定义文件。
  2. 从 stdcall 函数中删除下划线前缀。
  3. 使用 dlltool 将 MSVC 库转换为具有新定义的 MinGW 库。

那没用。我们最终从函数名称中删除了序数,这导致它可以编译。但是程序无法运行,因为它在 DLL 中找不到链接的函数。最后,在查阅了定义文件的 MSDN 文档后,我们更改了构建说明:

  1. 使用 reimp 创建定义文件。
  2. 为每个 stdcall 函数(格式为 _name@ordinal)添加一个行名 = _name@ordinal,允许 MinGW 将其 stdcall 命名约定映射到 MSVC 的命名约定。
  3. 使用 dlltool 将 MSVC 库转换为具有新定义的 MinGW 库。

成功了!要编译项目,您必须简单地:

  1. 下载并安装 Qt/Windows 包,其中包括 MinGW。
  2. 下载 reimp 并将其放到 MinGW/bin 文件夹中。
  3. 下载第三方库的开发包并将环境变量指向该位置。
  4. 使用常用的 qmake/make 命令构建项目。

取自: http://blog.outofhanwell.com/2006/05/01/linking-msvc-libraries-with-mingw-projects/

【讨论】:

  • 我目前正在尝试 QT 的 Visual Studio 插件,尝试构建 QT 应用程序并使用 VS 编译器(并使用我的 MSVC .libs)
【解决方案3】:

我假设您在另一个有问题的应用程序中使用了 MessageBuffer 库。该错误看起来像是找不到库或未导出 MessageBuffer 类。

你有没有试过把 -l 放在 pro 文件的库前面?

win32:LIBS += -lE:/SharedLibrary/lib/MessageBufferd.lib

见我的另一个answer。我添加了另一个答案,因为我不想让这个答案变得比现在更混乱。

到目前为止尝试过:

  • 不是拼写错误,d 已附加到库中
  • 使用 lib 扩展是正确的,如输出所示

【讨论】:

  • d是实际名称,d是为.lib的调试版本添加的
  • @xcimo 有道理...只是想检查一下
  • 不知道MessageBuffer类是否被导出。我在 messageBuffer 类中没有任何“export”关键字,因为我已经能够在许多其他 Visual Studio C++ 项目中使用这个库。静态库是否需要导出?或者可能是mingw需要的东西?感谢您的帮助。
  • 老实说,不确定它是否需要它......但错误似乎暗示它们无法找到。
  • 你认为它没有找到 .h 或 .lib 吗?
猜你喜欢
  • 2020-09-23
  • 1970-01-01
  • 1970-01-01
  • 2022-10-20
  • 1970-01-01
  • 2019-06-04
  • 2021-11-01
  • 2012-10-19
  • 1970-01-01
相关资源
最近更新 更多