在Windows下使用MinGW-w64编译Qt5 hello world .cpp文件会给出“未定义的引用".错误 [英] Compiling Qt5 hello world .cpp file under Windows using MinGW-w64 gives "undefined reference" error
问题描述
当我运行make
编译由cmake
生成的Makefile,编译Qt5应用程序的hello world示例时,编译失败并出现以下错误:
When I run make
to compile the Makefile produced by cmake
, to compile an hello world example of a Qt5 application, the compilation fails with the following error:
为什么编译失败?
(详细说明我要尝试执行的操作)
(details of what exactly I'm trying to do follow)
我在Windows 10下,使用从 Qt官方网站下载的Qt5.5二进制文件,mingw-w64 gcc
和g++
随 WinBuilds 一起提供,并且从官方网站下载的cmake
v3.6,并与Windows win64-x64 Installer
一起安装.
I'm under Windows 10, using the Qt5.5 binaries downloaded from Qt official website, mingw-w64 gcc
and g++
shipped with WinBuilds, and cmake
v3.6 downloaded from the official website and installed with the Windows win64-x64 Installer
.
我正尝试编译 Qt5官方Wiki 中提供的以下hello world测试文件:
I'm trying to compile the following hello world test file, provided in Qt5's official wiki:
#include <QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication app (argc, argv);
return app.exec();
}
使用命令cmake -G "MinGW Makefiles" ..
从包含.cpp文件的目录内名为build
的文件夹成功构建了Makefile.以下CMakeLists.txt文件(摘自 Qt5的cmake Wiki页面,加上了CMAKE_PREFIX_PATH变量的规范,例如在本 SO帖子中讨论的那样,这是必需的:
The Makefile is built successfully using the command cmake -G "MinGW Makefiles" ..
from a folder called build
inside the directory containing the .cpp file. The following CMakeLists.txt file (taken from Qt5's cmake wiki page, with the addition of the specification of the CMAKE_PREFIX_PATH variable, which is required as for example discussed in this SO post) was used:
cmake_minimum_required(VERSION 2.8.11)
SET(CMAKE_C_COMPILER C:/WinBuilds/bin/x86_64-w64-mingw32-gcc-4.8.3.exe)
SET(CMAKE_CXX_COMPILER C:/WinBuilds/bin/x86_64-w64-mingw32-g++-4.8.3.exe)
project(testproject)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
#set(CMAKE_PREFIX_PATH "C:/WinBuilds/lib64/cmake")
set(CMAKE_PREFIX_PATH "C:/Qt/5.5/mingw492_32/lib/cmake")
find_package(Qt5Widgets)
add_executable(testfile WIN32 test.cpp)
target_link_libraries(testfile Qt5::Widgets)
(我没有使用Qt5随附的cmake,因为那不起作用).
(I did not use the cmake shipped with Qt5 as that did not work).
现在,当我在cmake生成的Makefile上运行make
(或更准确地说,是mingw32-make
,又随WinBuilds一起提供)时,就会出现问题.
当我这样做时,编译失败并显示以下错误(上面的屏幕快照中显示了相同的错误):
Now, the problem arises when I run make
(or more precisely, mingw32-make
, again shipped with WinBuilds) on the Makefile produced by cmake.
When I do this, the compilation fails with the following error (same one showed in the screenshot above):
CMakeFiles\testfile.dir/objects.a(test.cpp.obj):test.cpp:(.text+0x35): undefined reference to `__imp__ZN16QCoreApplicationC1ERiPPci'
CMakeFiles\testfile.dir/objects.a(test.cpp.obj):test.cpp:(.text+0x3e): undefined reference to `__imp__ZN16QCoreApplication4execEv'
CMakeFiles\testfile.dir/objects.a(test.cpp.obj):test.cpp:(.text+0x50): undefined reference to `__imp__ZN16QCoreApplicationD1Ev'
CMakeFiles\testfile.dir/objects.a(test.cpp.obj):test.cpp:(.text+0x67): undefined reference to `__imp__ZN16QCoreApplicationD1Ev'
c:/winbuilds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\testfile.dir/objects.a(test.cpp.obj): bad reloc address 0xc in section `.xdata'
c:/winbuilds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
CMakeFiles\testfile.dir\build.make:127: recipe for target 'testfile.exe' failed
mingw32-make[2]: *** [testfile.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/testfile.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/testfile.dir/all] Error 2
Makefile:82: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
为什么编译失败?
在另一个 SO问题中报告了类似的未定义参考错误,但原因是似乎与本案有所不同.
A similar undefined reference error was reported in this other SO question, but the reason there seemed to be different than the present case.
推荐答案
问题是,当您尝试构建64位Qt应用程序时,您正在使用32位mingw编译的Qt二进制文件.您需要使用通过mingw编译的64位Qt二进制文件才能正常工作.
The problem is you are using 32 bit mingw compiled Qt binaries when you are trying to build a 64 bit Qt application. You need to use 64 bit Qt binaries compiled with mingw for this to work.
这篇关于在Windows下使用MinGW-w64编译Qt5 hello world .cpp文件会给出“未定义的引用".错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!