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/cmakebrew unlink cmake && brew link cmake
内容的提问来源于stack exchange,提问作者JanDuchscherer
相关产品推荐
相关产品推荐

