使用MinGW-W64 gcc 12.2.0编译Qt5.15.2时出现未定义引用链接错误
编译Qt 5.15.2(MinGW-W64 GCC 12.2.0)链接错误解决方案
问题概述
使用MinGW-W64 GCC 12.2.0编译Qt 5.15.2时,在最后链接阶段触发以下未定义引用错误:
C:/MinGW-w64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: .obj/release/qt5quicktest_qmltyperegistrations.o:qt5quicktest_qmltyperegistrations.cpp:(.rdata$.refptr._ZN11QQmlPrivate12ConstructorsI15QTestRootObjectLb1EE23createSingletonInstanceE[.refptr._ZN11QQmlPrivate12ConstructorsI15QTestRootObjectLb1EE23createSingletonInstanceE]+0x0): undefined reference to `QQmlPrivate::Constructors<QTestRootObject, true>::createSingletonInstance'
已完成的前置补丁:
- 修改
C:\Dev\qt5\qtbase\src\corelib\io\qfilesystemengine_win.cpp的条件编译:
将
改为#if defined(Q_CC_MINGW) && WINVER < 0x0602 // Windows 8 onwards#if defined(Q_CC_MINGW) && WINVER < 0x0602 && _WIN32_WINNT < _WIN32_WINNT_WIN8 // Windows 8 onwards - 为以下文件添加
#include <limits>:C:\Dev\qt5\qtbase\src\3rdparty\angle\src\libANGLE\HandleAllocator.cppC:\Dev\qt5\qtbase\src\corelib\text\qbytearraymatcher.hC:\Dev\qt5\qtbase\src\corelib\global\qfloat16.hC:\Dev\qt5\qtdeclarative\src\3rdparty\masm\yarr\Yarr.h
使用的configure命令:
configure -release -opensource -confirm-license -nomake tests -strip -c++std c++11 -skip qtwebengine -prefix %CD%\gcc_dll
解决方案
方法1:直接跳过QtQuickTest模块
在configure命令中追加-skip qtquicktest参数,彻底排除该模块的编译:
configure -release -opensource -confirm-license -nomake tests -strip -c++std c++11 -skip qtwebengine -skip qtquicktest -prefix %CD%\gcc_dll
执行后重新运行mingw32-make即可完成剩余编译。
方法2:修改QtQuickTest的编译配置
若需要保留该模块,可手动调整其编译规则:
- 打开
C:\Dev\qt5\qtdeclarative\src\quicktest\qquicktest.pro - 添加
CONFIG += no_singleton,禁用单例相关代码生成 - 重新执行
mingw32-make
方法3:降低GCC版本
Qt 5.15.2发布时GCC 12尚未推出,兼容性存在局限。建议切换到GCC 11.x版本编译,可从根源避免这类C++标准兼容问题。
原因说明
该错误源于Qt 5.15.2的QML单例构造模板在GCC 12的C++11编译模式下未能正确实例化,导致链接时缺失符号。注意-nomake tests仅跳过Qt自带测试用例的编译,不会排除QtQuickTest这个测试框架模块,因此需要单独指定跳过。
内容的提问来源于stack exchange,提问作者Tomay
相关产品推荐
相关产品推荐

