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

Win10下Rust交叉编译aarch64/Linux目标crate编译报错排查

Windows环境下Rust交叉编译到aarch64-unknown-linux-gnu目标问题记录

我尝试在Windows系统上为64位ARMv8架构的Raspbian(实际使用DietPi发行版)进行Rust程序交叉编译,但编译第三方crate时遇到了一系列问题。

已安装环境

  • rust toolchain 1.61.0
  • ARMv8 gcc toolchain(适配Raspberry Pi 64位版本)
  • MS Visual Studio 2019 C++ build tools
  • IntelliJ IDEA UE及对应Rust插件(仅作信息说明,与问题无直接关联)

安装完上述工具后,添加所需编译目标(注意该目标依赖ARM gcc toolchain,例如32位对应目标为armv7-unknown-linux-gnueabihf),执行命令:

rustup target add aarch64-unknown-linux-gnu

随后编辑%USERPROFILE%\.cargo\config文件,添加如下配置:

[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc.exe"

最后将以下路径添加到系统PATH环境变量:

  • %USERPROFILE%\.cargo\bin
  • c:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin(提供cmake命令)
  • c:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin(提供msbuild命令)
  • [ARMv8 gcc toolchain安装目录]\bin

交叉编译操作流程

确认项目在Windows平台可正常编译运行后,执行交叉编译命令:

cargo build -r --target=aarch64-unknown-linux-gnu

遇到的问题

上述配置对简单应用可正常编译,但编译包含较多依赖的项目时大量crate编译失败,以使用的plotters依赖的freetype-sys为例,报错如下:

error: failed to run custom build command for `freetype-sys v0.13.1`

Caused by:
  process didn't exit successfully: `D:\projects\rust\humidity\rh\target\release\build\freetype-sys-4feef64f7ae6c484\build-script-build` (exit code: 101)
  --- stdout

[...]
  
  running: "cmake" "[...]\\freetype-sys-0.13.1\\freetype2" "-DWITH_BZip2=OFF" "-DWITH_HarfBuzz=OFF" "-DWITH_PNG=OFF" "-DWITH_ZLIB=OFF" "-DCMAKE_INSTALL_PREFIX=D:\\projects\\rust\\humidity\\rh\\target\\aarch64-unknown-linux-gnu\\release\\build\\freetype-sys-3464f88f9fbe3bc0\\out" "-DCMAKE_C_FLAGS= -ffunction-sections -fdata-sections -fPIC" "-DCMAKE_CXX_FLAGS= -ffunction-sections -fdata-sections -fPIC" "-DCMAKE_ASM_FLAGS= -ffunction-sections -fdata-sections -fPIC" "-DCMAKE_BUILD_TYPE=Release"
  -- Building for: Visual Studio 15 2017
  -- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.19043.
  -- Configuring incomplete, errors occurred!
  See also "D:/projects/rust/humidity/rh/target/aarch64-unknown-linux-gnu/release/build/freetype-sys-3464f88f9fbe3bc0/out/build/CMakeFiles/CMakeOutput.log".

  --- stderr
  CMake Error at CMakeLists.txt:119 (project):
    Failed to run MSBuild command:

      MSBuild.exe

    to get the value of VCTargetsPath:

      Microsoft (R) Build Engine version 16.0.462+g62fb89029d for .NET Framework
      Copyright (C) Microsoft Corporation. All rights reserved.

      Build started 11/06/2022 11:53:19.
      Project "D:\projects\rust\humidity\rh\target\aarch64-unknown-linux-gnu\release\build\freetype-sys-3464f88f9fbe3bc0\out\build\CMakeFiles\3.13.19031502-MSVC_2\VCTargetsPath.vcxproj" on node 1 (default targets).
      c:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Microsoft\VC\v160\Microsoft.CppBuild.targets(378,5): error MSB8020: The build tools for Visual Studio 2017 (Platform Toolset = 'v141') cannot be found. To build using the v141 build tools, please install Visual Studio 2017 build tools.  Alternatively, you may upgrade to the current Visual Studio tools by selecting the Project menu or right-click the solution, and then selecting "Retarget solution". [D:\projects\rust\humidity\rh\target\aarch64-unknown-linux-gnu\release\build\freetype-sys-3464f88f9fbe3bc0\out\build\CMakeFiles\3.13.19031502-MSVC_2\VCTargetsPath.vcxproj]
      Done Building Project "D:\projects\rust\humidity\rh\target\aarch64-unknown-linux-gnu\release\build\freetype-sys-3464f88f9fbe3bc0\out\build\CMakeFiles\3.13.19031502-MSVC_2\VCTargetsPath.vcxproj" (default targets) -- FAILED.

此前其他crate编译时曾因缺少2019版MSBuild.exe路径报错,添加对应PATH后已解决,但本次报错提示需要MS VS 2017 build tools,判断问题并非单纯缺少对应版本构建工具。

已尝试的解决方案

注意到Visual Studio 2017(15.9版本)Build Tools内置了面向ARM64的交叉编译器,因此执行了以下操作:

  • 安装Visual Studio 2017 Build Tools
  • 运行vcvarsamd64_arm64.bat脚本配置编译环境
  • 将%USERPROFILE%\.cargo\config中配置的链接器从aarch64-linux-gnu-gcc.exe替换为MS编译器/链接器cl.exe
  • 在项目目录下执行cargo clean清理缓存
  • 重新执行cargo build -r --target=aarch64-unknown-linux-gnu

该配置下编译速度比使用gcc工具链快很多,但编译freetype crate时依旧失败,报错如下:

Compiling freetype v0.7.0
error: could not find native static library `freetype`, perhaps an -L flag is missing?

搭配gcc工具链和MS VC 2017环境测试,得到相同报错结果。

待解答疑问

Windows环境下交叉编译到aarch64-unknown-linux-gnu目标到底需要哪些配置?是否遗漏了必要步骤?具体疑问如下:

  1. 是否需要安装多个版本的VS build tools?多版本同时加入PATH是否会产生冲突?
  2. 是否需要单独安装cmake,而非使用VS内置的cmake版本?
  3. Windows环境下是否无法完成该目标的交叉编译?

后续发现版本停留在0.7.0、多年未更新的freetype crate本身可能存在交叉编译兼容问题,因此临时替换了项目中使用的plotters依赖,移除freetype相关依赖后交叉编译成功,但仍希望找到该问题的根本解决方案,目前推测方案可能需要提前编译对应目标架构的freetype库并配置编译时链接路径,流程相对复杂。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 17:15:45