树莓派4B+安装MediaPipe时Bazel工具链加载报错求助
问题背景
在搭载4GB内存、运行32位Raspberry Pi OS的树莓派4B+上,完成MediaPiPe前置依赖更新后,执行编译命令:
python3 setup.py gen_protos && python3 setup.py bdist_wheel
出现以下Bazel工具链错误:
ERROR: Traceback (most recent call last):
File "/home/diegos-pi/.cache/bazel/_bazel_diegos-pi/7f3a9a61ae329ba025208f264fd9a815/external/rules_cc/cc/private/rules_impl/compiler_flag.bzl", line 17, column 76, in
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain")
Error: file '@bazel_tools//tools/cpp:toolchain_utils.bzl' does not contain symbol 'use_cpp_toolchain' (did you mean 'find_cpp_toolchain'?)
ERROR: While resolving toolchains for target //mediapipe/modules/face_detection:face_detection_short_range_cpu: com.google.devtools.build.lib.packages.BuildFileContainsErrorsException: error loading package '@bazel_tools//tools/cpp': in /home/diegos-pi/.cache/bazel/_bazel_diegos-pi/7f3a9a61ae329ba025208f264fd9a815/external/rules_cc/cc/defs.bzl: Extension file 'cc/private/rules_impl/compiler_flag.bzl' has errors
ERROR: Analysis of target '//mediapipe/modules/face_detection:face_detection_short_range_cpu' failed; build aborted: com.google.devtools.build.lib.packages.BuildFileContainsErrorsException: error loading package '@bazel_tools//tools/cpp': in /home/diegos-pi/.cache/bazel/_bazel_diegos-pi/7f3a9a61ae329ba025208f264fd9a815/external/rules_cc/cc/defs.bzl: Extension file 'cc/private/rules_impl/compiler_flag.bzl' has errors
INFO: Elapsed time: 16.113s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (2 packages loaded, 0 targets configured)
错误原因
该错误源于Bazel版本与rules_cc依赖不兼容:rules_cc尝试调用的use_cpp_toolchain符号仅在Bazel 4.0.0及以上版本中存在,若当前安装的Bazel版本低于该阈值,就会触发此报错。
修复步骤
卸载现有Bazel
sudo apt remove -y bazel sudo rm -rf /usr/local/bin/bazel /usr/local/lib/bazel /home/$USER/.cache/bazel安装兼容版本的Bazel(推荐4.2.1)
- 下载对应armhf架构的
bazel-4.2.1-armhf.deb安装包 - 执行安装命令:
sudo dpkg -i bazel-4.2.1-armhf.deb
- 下载对应armhf架构的
验证Bazel版本
bazel --version确保输出为
bazel 4.2.1或其他4.x系列版本。重新执行编译命令
python3 setup.py gen_protos && python3 setup.py bdist_wheel
额外优化建议
树莓派内存有限,编译MediaPiPe时建议扩容swap分区至2GB,避免内存不足导致编译中断:
sudo dphys-swapfile swapoff sudo sed -i 's/CONF_SWAPSIZE=100/CONF_SWAPSIZE=2048/' /etc/dphys-swapfile sudo dphys-swapfile swapon
内容的提问来源于stack exchange,提问作者Diego Salvador

