Ubuntu 源码编译
DISTRO_TITLE_FULL 当前支持的基于 Debian 的目标平台如下:
- Tier 1:Ubuntu Linux - Resolute (26.04) 64 位
- Tier 3:Ubuntu Linux - Noble (24.04) 64 位
- Tier 3:Debian Linux - Trixie (13) 64 位
具体定义见 REP 2000。
设置 locale
Section titled “设置 locale”请确保你的系统拥有支持 UTF-8 的 locale。如果你处于最小化环境(例如 Docker 容器),locale 可能是 POSIX 这样的最小配置。我们使用以下设置进行测试。不过,只要你使用的是其他支持 UTF-8 的 locale,一般也没有问题。
locale # check for UTF-8
sudo apt update && sudo apt install localessudo locale-gen en_US en_US.UTF-8sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8export LANG=en_US.UTF-8
locale # verify settings启用所需的软件仓库
Section titled “启用所需的软件仓库”你需要将 ROS 2 apt 软件仓库添加到系统中。
首先确保已启用 Ubuntu Universe 仓库。
sudo apt install software-properties-commonsudo add-apt-repository universeros-apt-source 软件包为各种 ROS 仓库提供了密钥和 apt 源配置。
安装 ros2-apt-source 软件包后,系统将自动配置 ROS 2 仓库。当该软件包的新版本发布到 ROS 仓库时,仓库配置将自动更新。
sudo apt update && sudo apt install curl -yexport ROS_APT_SOURCE_VERSION=$(curl -s https://api.github.com/repos/ros-infrastructure/ros-apt-source/releases/latest | grep -F "tag_name" | awk -F'"' '{print $4}')curl -L -o /tmp/ros2-apt-source.deb "https://github.com/ros-infrastructure/ros-apt-source/releases/download/${ROS_APT_SOURCE_VERSION}/ros2-apt-source_${ROS_APT_SOURCE_VERSION}.$(. /etc/os-release && echo ${UBUNTU_CODENAME:-${VERSION_CODENAME}})_all.deb"sudo dpkg -i /tmp/ros2-apt-source.deb安装开发工具
Section titled “安装开发工具”$ sudo apt update && sudo apt install -y \ python3-mypy \ python3-pip \ python3-pytest \ python3-pytest-cov \ python3-pytest-mock \ python3-pytest-repeat \ python3-pytest-rerunfailures \ python3-pytest-runner \ python3-pytest-timeout \ ros-dev-tools构建 ROS 2
Section titled “构建 ROS 2”获取 ROS 2 代码
Section titled “获取 ROS 2 代码”创建一个工作空间并克隆所有仓库:
mkdir -p ~/ros2_{DISTRO}/srccd ~/ros2_{DISTRO}vcs import --input https://raw.githubusercontent.com/ros2/ros2/{REPOS_FILE_BRANCH}/ros2.repos src使用 rosdep 安装依赖
Section titled “使用 rosdep 安装依赖”ROS 2 软件包基于频繁更新的 Ubuntu 系统构建。在安装新软件包之前,建议始终确保系统已更新到最新状态。
sudo apt upgradesudo rosdep initrosdep updaterosdep install --from-paths src --ignore-src -y --skip-keys "fastcdr rti-connext-dds-7.7.0 urdfdom_headers"注意:如果你使用的是基于 Ubuntu 的发行版(如 Linux Mint),但系统并未将自身标识为 Ubuntu,你将收到类似 Unsupported OS [mint] 的错误信息。在这种情况下,请在上述命令后追加 --os=ubuntu:resolute。
安装额外的 RMW 实现(可选)
Section titled “安装额外的 RMW 实现(可选)”ROS 2 默认使用的中间件是 Fast DDS,但中间件(RMW)可以在构建时或运行时替换。关于如何使用多种 RMW,请参阅 使用多种 RMW 实现 指南。
安装 colcon mixins
Section titled “安装 colcon mixins”colcon mixin add default https://github.com/colcon/colcon-mixin-repository/raw/master/index.yamlcolcon mixin update default构建工作空间中的代码
Section titled “构建工作空间中的代码”如果你已经通过其他方式(通过 deb 包或二进制发行版)安装了 ROS 2,请确保在运行以下命令时,你处于一个全新的环境中,且没有 source 过这些其他安装。同时,请确保你的 .bashrc 中没有 source /opt/ros/${ROS_DISTRO}/setup.bash。你可以通过命令 printenv | grep -i ROS 来确认 ROS 2 没有被 source。其输出应该为空。
关于使用 ROS 工作空间的更多信息,请参阅相关教程。
cd ~/ros2_{DISTRO}/colcon build --symlink-install --mixin release注意:如果在编译所有示例时遇到问题导致无法成功完成构建,可以使用
--packages-skipcolcon 参数来跳过导致问题的软件包。例如,如果你不想安装庞大的 OpenCV 库,可以使用以下命令跳过依赖于它的软件包:
colcon build --symlink-install --packages-skip image_tools intra_process_demo通过 source 以下文件来设置你的环境:
. ~/ros2_{DISTRO}/install/local_setup.bash注意:如果你使用的不是 bash,请将
.bash替换为你的 shell 对应的文件。可选值为:setup.bash、setup.sh、setup.zsh。
在一个终端中,source setup 文件,然后运行一个 C++ talker:
. ~/ros2_{DISTRO}/install/local_setup.bashros2 run demo_nodes_cpp talker在另一个终端中 source setup 文件,然后运行一个 Python listener:
. ~/ros2_{DISTRO}/install/local_setup.bashros2 run demo_nodes_py listener你应该会看到 talker 显示它正在发布(Publishing)消息,而 listener 显示它收到了(I heard)这些消息。这验证了 C++ 和 Python API 都能正常工作。
继续阅读教程,配置环境,创建自己的工作空间和软件包,并学习 ROS 2 的核心概念。
使用 gcc 以外的编译器来编译 ROS 2 很容易。只需将环境变量 CC 和 CXX 分别设置为可用的 C 和 C++ 编译器可执行文件,然后重新触发 CMake 配置(使用 --cmake-force-configure,或删除你想影响的软件包),CMake 就会重新配置并使用新的编译器。
配置 CMake 以检测并使用 Clang:
sudo apt install clangexport CC=clangexport CXX=clang++colcon build --cmake-force-configure请参阅”维护源码检出”文档,定期刷新你的源码安装。
故障排除技巧请参阅安装故障排除一节。
-
如果你按照上述说明使用 colcon 安装了工作空间,“卸载”只需打开一个新终端,不 source 该工作空间的
setup文件即可。这样,你的环境将表现为系统中没有安装DISTRO_TITLE。 -
如果你还想释放空间,可以删除整个工作空间目录:
Terminal window rm -rf ~/ros2_{DISTRO}