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

Colcon指定CMake路径问题:MacOS下ROS 2编译报错求助

MacOS下ROS 2编译时Colcon调用错误CMake路径的问题

问题详情

在MacOS上安装ROS 2 Humble时,执行编译命令:

colcon build --symlink-install --packages-skip-by-dep python_qt_binding

触发错误,提示找不到旧版本的CMake路径:

make: /opt/homebrew/Cellar/cmake/3.25.0/bin/cmake: No such file or directory

完整编译输出如下:

Starting >>> ament_package
Starting >>> ament_lint
Starting >>> gtest_vendor
Starting >>> fastcdr
Starting >>> ament_cppcheck
Starting >>> iceoryx_hoofs
Starting >>> osrf_pycommon
Starting >>> osrf_testing_tools_cpp
--- stderr: fastcdr
make: /opt/homebrew/Cellar/cmake/3.25.0/bin/cmake: No such file or directory
make: *** [cmake_check_build_system] Error 1
---
Failed <<< fastcdr [0.50s, exited with code 2]
Aborted <<< osrf_testing_tools_cpp [0.49s]
Aborted <<< osrf_pycommon [0.50s]
Aborted <<< ament_cppcheck [0.51s]
Aborted <<< ament_lint [0.53s]
Aborted <<< ament_package [0.54s]
Aborted <<< gtest_vendor [0.53s]
Aborted <<< iceoryx_hoofs [0.68s]

Summary: 0 packages finished [2.13s]
  1 package failed: fastcdr
  7 packages aborted: ament_cppcheck ament_lint ament_package gtest_vendor iceoryx_hoofs osrf_pycommon osrf_testing_tools_cpp
  3 packages had stderr output: fastcdr gtest_vendor iceoryx_hoofs
  310 packages not processed
[2.326s] ERROR:colcon.colcon_notification.desktop_notification.terminal_notifier:Could not find the colcon-terminal-notifier.app in the install prefix '/opt/homebrew'

当前系统中正确的CMake路径为:

$ which cmake
/opt/homebrew/Cellar/cmake/3.26.0/bin/cmake

尝试执行set -x CMAKE_COMMAND /opt/homebrew/Cellar/cmake/3.26.0/bin设置路径,但无效果。

解决方法

  • 清理编译缓存:Colcon的缓存文件会保留旧的CMake路径记录,删除相关目录后重新编译:
    rm -rf build install log
    
  • 优先加载正确的CMake路径:将正确的CMake目录添加到系统PATH最前端,确保系统优先调用新版本:
    export PATH="/opt/homebrew/Cellar/cmake/3.26.0/bin:$PATH"
    
    若要永久生效,将上述命令添加到~/.zshrc(Zsh)或~/.bash_profile(Bash)中,再执行source ~/.zshrc或source ~/.bash_profile刷新环境变量。
  • 直接给Colcon指定CMake路径:在编译命令中通过--cmake-args参数强制指定CMake路径:
    colcon build --symlink-install --packages-skip-by-dep python_qt_binding --cmake-args -DCMAKE_COMMAND=/opt/homebrew/Cellar/cmake/3.26.0/bin/cmake
    
  • 修复Homebrew的CMake软链接:Homebrew默认会在/opt/homebrew/bin创建CMake的软链接,检查链接是否指向正确版本:
    ls -l /opt/homebrew/bin/cmake
    
    如果指向旧版本,重新链接:
    brew unlink cmake && brew link cmake
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 11:17:57