简体   繁体   English

Qt / mingw32未定义的引用错误...无法链接.lib

[英]Qt/mingw32 undefined reference errors… unable to link a .lib

I am new to Qt and have one error I am unable to fix. 我是Qt的新手,有一个我无法解决的错误。

I have a bunch of windows (VS2005) static library file ( .lib ). 我有一堆windows(VS2005)静态库文件( .lib )。 And I am testing if they work well with Qt. 我正在测试它们是否与Qt配合良好。 So I took the most simple library that I have. 所以我选择了最简单的库。 (Called MessageBuffer ). (称为MessageBuffer )。

So I added MessageBuffer.h to the main.cpp , and added the location of those file in the INCLUDEPATH of the .pro . 所以我将MessageBuffer.h添加到main.cpp ,并在.proINCLUDEPATH中添加了这些文件的位置。

Until then everything seem fine, I can use the class and Qt IDE show all method and everything. 在那之前一切都很好,我可以使用类和Qt IDE显示所有方法和一切。 So to me it look like it found the .h file. 所以对我来说,它看起来像是找到了.h文件。

Now I added the MessageBuffer.lib (VS2005/Debug build) in the .pro like this: 现在我在.pro添加了MessageBuffer.lib (VS2005 / Debug build),如下所示:

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

I have also tried the following: 我也尝试过以下方法:

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

Here is the content of my .pro file: 这是我的.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

They all give me the same errors: (and I get the same even if I don't include the .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'

Can anyone help please? 有人可以帮忙吗?

Based on the question Use libraries compiled with visual studio in an application compiled by g++ (mingw) and the MSDN forum post I can't mix VC & GCC it does not appear you can link a gcc application with visual c++ compiled libraries. 基于在g ++(mingw)和MSDN论坛帖子编译的应用程序中使用可视化工作室编译的库, 我不能混合VC和GCC ,看起来你不能将gcc应用程序与visual c ++编译库链接。

The solution would be to recompile everything with the same compiler. 解决方案是使用相同的编译器重新编译所有内容。

The MinGW FAQ discusses this problem and offers a solution: MinGW FAQ讨论了这个问题,并提供了一个解决方案:

  1. Create a definition file using reimp (for lib files) or pexports (for dll files). 使用reimp(用于lib文件)或pexports(用于dll文件)创建定义文件。
  2. Remove the underscore prefixes from the stdcall functions. 从stdcall函数中删除下划线前缀。
  3. Use dlltool to convert the MSVC library into a MinGW library with the new definition. 使用dlltool将MSVC库转换为具有新定义的MinGW库。

That didn't work. 那没用。 We finally removed the ordinals from the function names, which caused it to compile. 我们最终从函数名中删除了序数,这导致它编译。 But the program wouldn't run because it couldn't find the linked functions in the DLL. 但程序无法运行,因为它无法在DLL中找到链接的函数。 Finally, after consulting the MSDN documentation for definition files, we changed the build instructions: 最后,在查阅定义文件的MSDN文档后,我们更改了构建说明:

  1. Create a definition file using reimp. 使用reimp创建定义文件。
  2. For each stdcall function (formatted as _name@ordinal) add a line name = _name@ordinal, allowing MinGW to map its stdcall naming convention to that of MSVC. 对于每个stdcall函数(格式为_name @ ordinal),添加一个行名= _name @ ordinal,允许MinGW将其stdcall命名约定映射到MSVC的命名约定。
  3. Use dlltool to convert the MSVC library into a MinGW library with the new definition. 使用dlltool将MSVC库转换为具有新定义的MinGW库。

It worked! 有效! To compile the project you must simply: 要编译项目,您必须简单地:

  1. Download and install the Qt/Windows package, which includes MinGW. 下载并安装Qt / Windows软件包,其中包括MinGW。
  2. Download reimp and drop it into the MinGW/bin folder. 下载reimp并将其放入MinGW / bin文件夹。
  3. Download the development packages for the third-party libraries and point an environment variable to that location. 下载第三方库的开发包,并将环境变量指向该位置。
  4. Build the project with the usual qmake/make commands. 使用通常的qmake / make命令构建项目。

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

I assume that you have used the MessageBuffer library in another application with problems. 我假设你在另一个有问题的应用程序中使用了MessageBuffer库。 The error looks like it either cannot find the library or the MessageBuffer class is not being exported. 该错误看起来无法找到库或未导出MessageBuffer类。

Have you tried putting -l on front of the library in the pro file? 您是否尝试在专业文件中将-l放在库的前面?

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

See my other answer . 看到我的其他答案 I added the other answer because I didn't want to make this answer any more messy than it already was. 我添加了另一个答案,因为我不想让这个答案比现在更混乱。

Tried so far: 到目前为止尝试过:

  • Not a typo, d is appended to the library 不是拼写错误,d附加到库中
  • Using the lib extension is correct as seen in the output 使用lib扩展是正确的,如输出中所示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM