You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows10编译Qt源码遇架构不兼容及MySQL驱动问题求助

Windows10下Qt安装与QtSQL MySQL插件问题排查及解决

问题背景

  • 初始采用在线安装器部署Qt与Qt Creator,无法添加所需的QtSQL MySQL插件,遂转向源码编译方案。
  • 多版本在线安装尝试均失败:
    • Qt 6.7.2/6.5.3:Qt\版本号\mingw\plugins\sqldrivers目录为空,MySQL驱动初始化失败
    • Qt 6.6.3:sqldrivers目录非空,但驱动初始化后编译失败
  • 源码编译尝试记录:
    • Qt v6.6.0:通过perl执行init-repository后编译失败
    • Qt v6.8.0-beta1:该版本自带init-repository.bat适配Windows环境,已配置好CMake、Ninja、MSVC 2022/2019(均为amd64架构)、x64版本MinGW 13.2.0,执行以下命令后出现架构不兼容错误:
      git clone --branch v6.8.0-beta1 code.qt.io/qt/qt5.git Qt
      configure -init-submodules
      cmake --build .
      
      错误日志:
      C:\Qt>cmake --build .
      [26/11645] Linking CXX executable qtbase\bin\syncqt.exe
      FAILED: qtbase/bin/syncqt.exe
      C:\Windows\system32\cmd.exe /C "cd . && C:\MinGW\bin\c++.exe -DNDEBUG -O3 -fexceptions  qtbase/src/tools/syncqt/CMakeFiles/syncqt.dir/syncqt_resource.rc.obj qtbase/src/tools/syncqt/CMakeFiles/syncqt.dir/main.cpp.obj qtbase/src/tools/syncqt/CMakeFiles/syncqt.dir/syncqt_longpath.rc.obj -o qtbase\bin\syncqt.exe -Wl,--out-implib,qtbase\src\tools\syncqt\libsyncqt.dll.a -Wl,--major-image-version,0,--minor-image-version,0  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
      C:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: i386 architecture of input file `qtbase/src/tools/syncqt/CMakeFiles/syncqt.dir/syncqt_resource.rc.obj' is incompatible with i386:x86-64 output
      C:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: i386 architecture of input file `qtbase/src/tools/syncqt/CMakeFiles/syncqt.dir/syncqt_longpath.rc.obj' is incompatible with i386:x86-64 output
      collect2.exe: error: ld returned 1 exit status
      [43/11645] Building C object qtbase/src/3rdparty/libjpeg/CMakeFiles/BundledLibjpeg.dir/jdhuff.c.obj
      ninja: build stopped: subcommand failed.
      
    • Qt 6.7.2源码:执行perl init-repository、cmake --build . --parallel、cmake --install .后,初始化MySQL驱动时出现CMake语法错误、路径解析错误。

解决方法

  1. 统一编译架构
    确保所有工具链(MSVC、MinGW、CMake)均为x64架构,避免32位与64位编译产物混合。针对资源文件(.rc)编译不兼容问题,可在CMake配置时添加参数强制生成64位目标:

    cmake -DCMAKE_RC_FLAGS="-F x64" ..
    
  2. 在线安装后补装MySQL驱动(无需源码编译)

    • 下载与Qt版本、架构、编译器匹配的MySQL Connector/C(注意选择64位版本)
    • 进入Qt安装目录下的qtbase/src/plugins/sqldrivers文件夹,执行以下命令:
      qmake "INCLUDEPATH+=C:/path/to/mysql-connector/include" "LIBS+=C:/path/to/mysql-connector/lib/libmysql.lib" mysql.pro
      nmake  # MinGW环境用mingw32-make
      
    • 将编译生成的qsqlmysql.dll、qsqlmysqld.dll复制到Qt\版本号\编译器\plugins\sqldrivers目录
    • 将MySQL Connector中的libmysql.dll复制到Qt的bin目录或系统PATH可访问路径
  3. 源码编译规范

    • 编译前清理所有CMake缓存文件,避免残留配置冲突
    • 环境变量中仅保留当前使用的编译器(MSVC或MinGW)路径,防止交叉编译干扰
    • 使用configure时明确指定编译器与MySQL依赖路径,例如MinGW环境:
      configure -prefix C:/Qt/6.7.2/mingw_64 -release -opensource -confirm-license -platform win32-g++ -sql-mysql "MYSQL_INCDIR=C:/path/to/mysql/include" "MYSQL_LIBDIR=C:/path/to/mysql/lib"
      

内容的提问来源于stack exchange,提问作者ajitest

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 05:49:52