简体   繁体   English

在Windows下使用MinGW-w64编译Qt5 hello world .cpp文件会产生“未定义引用”错误

[英]Compiling Qt5 hello world .cpp file under Windows using MinGW-w64 gives “undefined reference” error

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: 当我运行make编译cmake生成的Makefile,以编译Qt5应用程序的hello world示例时,编译失败并显示以下错误:

在此处输入图片说明

Why is the compilation failing? 为什么编译失败?

(details of what exactly I'm trying to do follow) (详细说明我要尝试执行的操作)


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 . 我在Windows 10下,使用从Qt官方网站下载的Qt5.5二进制文件, WinBuilds附带的mingw-w64 gccg++ ,以及从官方网站下载并与Windows win64-x64 Installer程序一起安装的cmake v3.6

I'm trying to compile the following hello world test file, provided in Qt5's official wiki : 我正在尝试编译Qt5官方Wiki中提供的以下hello world测试文件:

#include <QCoreApplication>

int main(int argc, char *argv[])
{
    QCoreApplication app (argc, argv);

    return app.exec();
}

The Makefile is built successfully using the command cmake -G "MinGW Makefiles" .. from a folder called build inside the directory containing the .cpp file. 使用命令cmake -G "MinGW Makefiles" ..从包含.cpp文件的目录内部的名为build的文件夹成功cmake -G "MinGW Makefiles" .. 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: 使用了以下CMakeLists.txt文件(摘自Qt5的cmake Wiki页面 ,其中添加了CMAKE_PREFIX_PATH变量的规范,例如在本SO帖子中讨论的那样)。

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)

(I did not use the cmake shipped with Qt5 as that did not work). (我没有使用Qt5附带的cmake,因为那不起作用)。

Now, the problem arises when I run make (or more precisely, mingw32-make , again shipped with WinBuilds) on the Makefile produced by cmake. 现在,当我在由cmake生成的Makefile上运行make (或更精确地说, mingw32-make ,再次随WinBuilds发行)时,就会出现问题。 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

Why is the compilation failing? 为什么编译失败?

A similar undefined reference error was reported in this other SO question , but the reason there seemed to be different than the present case. 在另一个SO问题中报告了类似的未定义参考错误,但原因似乎与当前情况不同。

The problem is you are using 32 bit mingw compiled Qt binaries when you are trying to build a 64 bit Qt application. 问题是,当您尝试构建64位Qt应用程序时,您正在使用32位mingw编译的Qt二进制文件。 You need to use 64 bit Qt binaries compiled with mingw for this to work. 您需要使用通过mingw编译的64位Qt二进制文件才能正常工作。

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

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