本页内容

交叉编译

Qt VS Tools 通过集成 Qt/MSBuild 和 Linux 开发 VS 工作负载,支持 Qt 项目的交叉编译。这样,您就可以开发基于 widget 和Qt Quick 的应用程序,同时面向 Windows 和 Linux,包括嵌入式设备。

设置交叉编译

使用 Visual Studio 为 Linux 开发 Qt 应用程序:

  1. 安装 Desktop development with C++ 和 Linux development with C++ 工作负载。
  2. 启用 Windows Subsystem for Linux (WSL) 并安装 Linux 发行版。
  3. 或者,创建与远程 Linux 编译服务器的连接。
  4. 添加驻留在 WSL 实例或远程构建服务器中的交叉编译 Qt 构建。

添加用于交叉编译的 Qt 版本

添加交叉编译的 Qt 版本:

  1. 转到Extensions >Qt VS Tools >Qt Versions 。

    Qt 版本

  2. 选择Add 。
  3. 在Name 中,输入 Qt 版本的名称。如果先添加 Qt Location,该字段将自动填写。
  4. 在Location 中,输入 Qt 安装路径。
  5. 在Host 中,选择Linux WSL 或Linux SSH 。
  6. Compiler 显示要使用的交叉编译器。

在 WSL 上编译

使用 WSL 实例运行交叉编译器比使用 SSH 连接到编译主机更快。使用 SSH 时,MSBuild 必须先将源文件上传到远程主机,而 WSL 会将本地驱动器挂载到/mnt ,从而使文件访问即时完成。

MSBuild 假定选项中指定的路径是本地路径,并将其映射为根植于/mnt 的相应 Linux 路径。例如

C:\include\foolib maps to /mnt/c/include/foolib

使用绝对 Linux 路径作为Extensions >Qt VS Tools >Options >Qt/MSBuild 的值会导致错误的映射。例如,/home/user/raspi 映射到/mnt/home/user/raspi 。

为避免这一问题,请在/mnt 中创建指向 WSL 实例文件系统中其他顶级目录的符号链接。

另请参阅 教程:Qt Quick 调试、调试应用程序、在 Linux 上调试、启用 QML 调试、构建和调试Qt Quick 应用程序。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.