C++ Qt/mingw32 未定义的引用错误...无法链接 .lib
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1137323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Qt/mingw32 undefined reference errors... unable to link a .lib
提问by
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
). And I am testing if they work well with Qt. So I took the most simple library that I have. (Called MessageBuffer
).
我有一堆 Windows (VS2005) 静态库文件 ( .lib
)。我正在测试它们是否与 Qt 配合良好。所以我选择了我拥有的最简单的图书馆。(称为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
, 并INCLUDEPATH
在.pro
.
Until then everything seem fine, I can use the class and Qt IDE show all method and everything. So to me it look like it found the .h file.
在那之前一切似乎都很好,我可以使用类和 Qt IDE 显示所有方法和所有内容。所以对我来说它看起来像是找到了 .h 文件。
Now I added the MessageBuffer.lib
(VS2005/Debug build) in the .pro
like this:
现在我像这样添加了MessageBuffer.lib
(VS2005/Debug build).pro
:
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:\Qt09.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?
有人可以帮忙吗?
采纳答案by Jesse Vogt
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 & GCCit does not appear you can link a gcc application with visual c++ compiled libraries.
基于在由 g++ (mingw) 编译的应用程序中使用 Visual Studio 编译的库和 MSDN 论坛帖子我不能混合 VC 和 GCC 的问题,您似乎无法将 gcc 应用程序与 Visual c++ 编译库链接起来。
The solution would be to recompile everything with the same compiler.
解决方案是使用相同的编译器重新编译所有内容。
回答by Jesse Vogt
The MinGW FAQ discusses this problem and offers a solution:
MinGW FAQ 讨论了这个问题并提供了一个解决方案:
- Create a definition file using reimp (for lib files) or pexports (for dll files).
- Remove the underscore prefixes from the stdcall functions.
- Use dlltool to convert the MSVC library into a MinGW library with the new definition.
- 使用 reimp(对于 lib 文件)或 pexports(对于 dll 文件)创建定义文件。
- 从 stdcall 函数中删除下划线前缀。
- 使用 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. Finally, after consulting the MSDN documentation for definition files, we changed the build instructions:
那没有用。我们最终从函数名称中删除了序数,这导致它编译。但是程序无法运行,因为它在 DLL 中找不到链接的函数。最后,在查阅定义文件的 MSDN 文档后,我们更改了构建说明:
- Create a definition file using reimp.
- 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.
- Use dlltool to convert the MSVC library into a MinGW library with the new definition.
- 使用 reimp 创建定义文件。
- 对于每个 stdcall 函数(格式为 _name@ordinal),添加一个行名称 = _name@ordinal,允许 MinGW 将其 stdcall 命名约定映射到 MSVC 的命名约定。
- 使用 dlltool 将 MSVC 库转换为具有新定义的 MinGW 库。
It worked! To compile the project you must simply:
有效!要编译项目,您必须简单地:
- Download and install the Qt/Windows package, which includes MinGW.
- Download reimp and drop it into the MinGW/bin folder.
- Download the development packages for the third-party libraries and point an environment variable to that location.
- Build the project with the usual qmake/make commands.
- 下载并安装 Qt/Windows 包,其中包括 MinGW。
- 下载 reimp 并将其放入 MinGW/bin 文件夹中。
- 下载第三方库的开发包并将环境变量指向该位置。
- 使用通常的 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/
回答by Jesse Vogt
I assume that you have used the MessageBuffer library in another application with problems. The error looks like it either cannot find the library or the MessageBuffer class is not being exported.
我假设您在另一个有问题的应用程序中使用了 MessageBuffer 库。该错误看起来像是找不到库或 MessageBuffer 类没有被导出。
Have you tried putting -l on front of the library in the pro file?
您是否尝试过将 -l 放在 pro 文件中的库前面?
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
- Using the lib extension is correct as seen in the output
- 不是打字错误,d 被附加到库中
- 如输出所示,使用 lib 扩展名是正确的