# colcon

**colcon**（collective construction，集体构建）是一个命令行工具，用于简化多个软件包的构建、测试和使用流程；它将整个过程自动化，正确地排列软件包顺序，并准备好环境，使构建产物可以直接使用。<sup>[1](https://github.com/colcon/colcon-core)</sup> 它用 Python 编写，是[机器人操作系统](https://www.edgepedia.org/zh/robot-operating-system) [ROS 2](https://www.edgepedia.org/zh/ros-2) 选定的构建工具，在 ROS 2 中取代了早期的 `ament build` 和 `ament test` 命令。<sup>[2](https://design.ros2.org/articles/build_tool.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 用途 | 用于构建软件包集合的命令行工具，负责处理顺序和环境配置<sup>[1](https://github.com/colcon/colcon-core)</sup> |
| 实现 | Python 3.5+ 代码库；目标平台为 Linux、macOS 和 Windows<sup>[2](https://design.ros2.org/articles/build_tool.html)</sup> |
| 被选为 ROS 2 构建工具 | 在与 `catkin_tools` 的比较中被选为通用构建工具；`colcon build` / `colcon test` 取代了 `ament build` / `ament test`<sup>[2](https://design.ros2.org/articles/build_tool.html)</sup> |
| devel 空间 | 按设计不支持 ROS 1 的“devel 空间”；每个软件包都必须声明安装步骤<sup>[3](https://colcon.readthedocs.io/en/main/user/quick-start.html)</sup> |
| 默认目录 | 构建基础目录 `./build`，安装基础目录 `./install`；`--merge-install` 让所有软件包共用一个安装前缀<sup>[4](https://github.com/colcon/colcon.readthedocs.org/blob/main/reference/verb/build.rst)</sup> |
| 执行方式 | 默认并行构建软件包；`--executor sequential` 一次只构建一个软件包<sup>[5](https://nu-msr.github.io/ros_notes/ros2/colcon.html)</sup> |
| 打包 | `colcon-bundle` 扩展为已构建的工作空间生成可移植的 `bundle/output.tar`<sup>[6](https://github.com/colcon/colcon-bundle)</sup> |

## 在 ROS 中的角色

在与 ROS 1 许多开发者使用的构建工具 `catkin_tools` 进行设计比较之后，colcon 被选为 ROS 2 的通用构建工具。决策文章指出，当时 colcon 已具备为 ROS 2 部署的条件，其更清晰的架构、模块化、可扩展性以及 Python 3.5 代码库被视为长期优势。这一转换无需修改现有 ROS 软件包的 CMake 代码。<sup>[2](https://design.ros2.org/articles/build_tool.html)</sup> Bouncy 版本发布后，早期的 `ament_tools` 仓库被归档，并从 `ros2.repos` 文件中移除。<sup>[2](https://design.ros2.org/articles/build_tool.html)</sup>

尽管它是 ROS 2 的构建工具，colcon 并不专属于 ROS。它可以构建 ROS 1 工作空间、ROS 2 工作空间，以及带有 ignition 依赖的 Gazebo，并支持多种构建系统，包括 ament_cmake、ament_python、纯 CMake、Catkin 和 setuptools。<sup>[2](https://design.ros2.org/articles/build_tool.html)</sup><sup> • </sup><sup>[5](https://nu-msr.github.io/ros_notes/ros2/colcon.html)</sup> 每个软件包 `package.xml` 中声明的依赖决定构建顺序：如果软件包 A 声明了对软件包 B 的 `build_depend`，colcon 总是先构建 B 再构建 A。<sup>[5](https://nu-msr.github.io/ros_notes/ros2/colcon.html)</sup>

与 `catkin_make` 的一个显著区别是 colcon 没有 `devel` 目录。每个软件包都必须声明安装步骤，要使用已构建的工作空间，需要加载安装空间的环境设置，例如 `source install/setup.sh`。<sup>[7](https://aws.amazon.com/blogs/opensource/building-bundling-ros-app-aws-robomaker/)</sup><sup> • </sup><sup>[3](https://colcon.readthedocs.io/en/main/user/quick-start.html)</sup>

## 工作空间与构建选项

在工作空间中运行 `colcon build` 会将构建产物存入 `build/`，并将结果安装到 `install/`，代码实际从那里运行；工作空间至少需要构建一次才能使用。<sup>[5](https://nu-msr.github.io/ros_notes/ros2/colcon.html)</sup> 默认情况下，colcon 构建的是隔离工作空间，每个软件包都安装到安装空间中自己的子目录，例如 `install/foo`。隔离让测试只能看到已声明依赖的安装产物，从而能发现未声明的依赖，也允许通过删除目录来移除某个软件包。<sup>[8](https://colcon.readthedocs.io/en/released/user/isolated-vs-merged-workspaces.html)</sup>

使用 `--merge-install` 时，所有软件包安装到同一目录树中。这会缩短环境设置脚本设置的环境变量，因为一个条目就覆盖了所有已安装的软件包，这在环境变量长度受到严格限制的平台上很重要，例如 [Windows 10](https://www.edgepedia.org/zh/windows-10)。代价是失去隔离：声明对一个软件包的依赖，同时也会获得同一前缀下其他软件包资源的访问权。<sup>[8](https://colcon.readthedocs.io/en/released/user/isolated-vs-merged-workspaces.html)</sup><sup> • </sup><sup>[4](https://github.com/colcon/colcon.readthedocs.org/blob/main/reference/verb/build.rst)</sup> ROS 2 教程建议在 Windows 上使用 `--merge-install`，因为 Windows 不允许长路径，并指出在那里使用 `--symlink-install` 需要管理员权限或开发者模式。<sup>[9](https://github.com/ros2/ros2_documentation/blob/rolling/source/ROS-Framework/client-libraries/Working-with-Client-Libraries/Colcon-Tutorial.rst)</sup>

`--symlink-install` 尽可能使用符号链接，而不是从源目录和构建目录复制文件。<sup>[4](https://github.com/colcon/colcon.readthedocs.org/blob/main/reference/verb/build.rst)</sup> 由于 `ament_cmake` 等构建类型要求软件包被安装，这个选项允许通过编辑源文件（例如 Python 文件）来改变已安装的文件，从而加快迭代。<sup>[9](https://github.com/ros2/ros2_documentation/blob/rolling/source/ROS-Framework/client-libraries/Working-with-Client-Libraries/Colcon-Tutorial.rst)</sup> 对启动文件的修改仍然需要重新构建。<sup>[5](https://nu-msr.github.io/ros_notes/ros2/colcon.html)</sup>

**并行与失败处理。** colcon 默认并行构建软件包。如果依赖未声明，两个软件包可能同时构建，若依赖先完成，构建可能侥幸成功；用 `--executor sequential` 构建可以检验依赖是否声明正确。<sup>[5](https://nu-msr.github.io/ros_notes/ros2/colcon.html)</sup> `--continue-on-error` 参数在一个软件包失败时继续构建其他软件包，跳过递归依赖失败软件包的那些包。<sup>[4](https://github.com/colcon/colcon.readthedocs.org/blob/main/reference/verb/build.rst)</sup> 在 CPU、内存和 I/O 受限的系统上，例如[树莓派](https://www.edgepedia.org/zh/raspberry-pi)，并行构建可能导致屏幕和鼠标卡死，因此建议在那里使用顺序执行器。<sup>[9](https://github.com/ros2/ros2_documentation/blob/rolling/source/ROS-Framework/client-libraries/Working-with-Client-Libraries/Colcon-Tutorial.rst)</sup>

## 动词

功能按动词组织，以 `colcon <verb>` 的方式调用。

- **build** 构建一组软件包。它由 `colcon-core` 软件包提供，接受执行器、事件处理器、发现、软件包选择和 mixin 参数，以及构建系统特定的参数，例如来自 `colcon-cmake` 的 `--cmake-args` 和来自 `colcon-ros` 的 `--ament-cmake-args`。<sup>[4](https://github.com/colcon/colcon.readthedocs.org/blob/main/reference/verb/build.rst)</sup> 软件包选择参数包括用于单个软件包的 `--packages-select` 和用于某个软件包及其递归依赖的 `--packages-up-to`。<sup>[3](https://colcon.readthedocs.io/en/main/user/quick-start.html)</sup>
- **test** 运行已构建软件包的测试；软件包必须先构建才能测试。<sup>[10](https://roscon.ros.org/2019/talks/roscon2019_colcon.pdf)</sup>
- **test-result** 汇总先前运行的测试结果。它由 `colcon-test-result` 软件包提供，使用 `./build` 作为结果文件的默认基础路径，并提供 `--all`、`--verbose` 和 `--delete` 选项。<sup>[11](https://colcon.readthedocs.io/en/released/reference/verb/test-result.html)</sup>

最近一次调用的日志文件默认保存在 `~/.colcon/log/latest`，`--event-handlers console_cohesion+` 会在每个软件包完成后显示其输出。<sup>[3](https://colcon.readthedocs.io/en/main/user/quick-start.html)</sup>

## 扩展架构

colcon 由一系列扩展实现，任何人都可以编写扩展；常用扩展随 `python3-colcon-common-extensions` 软件包发布，普遍有用的扩展托管在 colcon GitHub 组织上。<sup>[5](https://nu-msr.github.io/ros_notes/ros2/colcon.html)</sup> 处理的每个阶段都是 `colcon-core` 中的一个扩展点：<sup>[12](https://colcon.readthedocs.io/en/released/developer/extension-point.html)</sup>

1. **软件包发现**扩展返回可能包含软件包的候选路径。
2. **软件包识别**扩展判断某个路径是否包含软件包，以及它的名称和类型，这决定它如何被处理；`colcon-ros` 提供基于 `package.xml` 文件的识别。
3. **软件包增强**添加识别无法提取的元数据，例如依赖。
4. **软件包选择**扩展根据命令行参数选择或取消选择软件包；默认选择所有软件包。
5. **任务**扩展实现某个动词与软件包类型组合的逻辑，例如构建一个 Python 软件包。
6. **执行器**扩展运行得到的作业集合；默认使用优先级最高的执行器。
7. **Shell** 和**环境**扩展为每种支持的 shell 生成环境设置脚本和环境钩子。

第三方扩展覆盖其他生态系统；例如，`colcon-cargo` 为 Rust cargo 软件包添加发现、识别、构建和测试支持。<sup>[13](https://github.com/colcon/colcon-cargo/blob/main/setup.cfg)</sup>

## 打包

`colcon-bundle` 扩展将已构建的工作空间打包成一个可移植环境，可以移动到不同的 Linux 系统上，并像其内容已本地安装一样执行。运行 `colcon bundle` 会从 `package.xml` 解析依赖，将 apt、pip 和 pip3 依赖安装到 bundle 中，将脚本的 shebang 重写为 `#!/usr/bin/env`，并按照有文档记录的 bundle 格式将工作空间和依赖打包成 `bundle/output.tar`。<sup>[6](https://github.com/colcon/colcon-bundle)</sup> 该扩展是为 [AWS RoboMaker](https://www.edgepedia.org/zh/aws-robomaker) 创建的，其开发者参照 appimage、flatpak 和 snapcraft 设计了这种单文件格式，并基于 colcon 构建工具链，因为它能构建 ROS 1 和 ROS 2 应用。<sup>[7](https://aws.amazon.com/blogs/opensource/building-bundling-ros-app-aws-robomaker/)</sup> bundle 扩展仅支持 x86、ARMHF 和 ARM64 架构上的 Ubuntu Xenial 和 Bionic。<sup>[6](https://github.com/colcon/colcon-bundle)</sup>

## 历史与当前开发

colcon 项目由德克·托马斯（Dirk Thomas）开发，他此前是 Open Robotics 的 ROS 2 负责人，最初是作为个人项目在 ROS 2 构建工具决策初稿之外进行的。<sup>[2](https://design.ros2.org/articles/build_tool.html)</sup><sup> • </sup><sup>[14](https://github.com/colcon/colcon-core/blob/master/bin/colcon)</sup> 核心工具的引导脚本带有他名下 2016 至 2018 年的版权声明。<sup>[14](https://github.com/colcon/colcon-core/blob/master/bin/colcon)</sup>

colcon-core 仓库中 2025 年的一项提案描述了向现代 Python 打包标准 PEP 517/518/621/660 的迁移。目前 colcon-core 通过静态解析 `setup.cfg` 来识别 Python 软件包，并通过调用 `setup.py build` 和 `setup.py install` 来构建它们，动态的 `setup.py` 代码由单独的 `colcon-python-setup-py` 扩展处理。该提案认为这种遗留模式面临风险，因为上游工具正在弃用直接调用 `setup.py install` 和 `setup.py develop`，而且 colcon 无法原生构建使用 Hatchling 或 Flit 等现代后端或 `pyproject.toml` 中声明的元数据的软件包。<sup>[15](https://github.com/colcon/colcon-core/issues/736)</sup> 一份关于工作空间级 Python 虚拟环境的相关实地报告发现，已知的 shebang 变通方案不覆盖 `--symlink-install` 代码路径，因此当 colcon 从系统 Python 启动时，目前对于基于 venv 的工作流没有完整的变通方案。<sup>[16](https://discourse.openrobotics.org/t/uv-on-ros-2-a-field-report-on-workspace-level-virtual-environments-five-failure-modes-and-minimal-colcon-ament-proposals/57111)</sup>

## References

1. [colcon-core: Command line tool to build sets of software packages, GitHub](https://github.com/colcon/colcon-core)
2. [A universal build tool, ROS 2 Design](https://design.ros2.org/articles/build_tool.html)
3. [Quick start, colcon documentation](https://colcon.readthedocs.io/en/main/user/quick-start.html)
4. [build - Build Packages, colcon documentation](https://github.com/colcon/colcon.readthedocs.org/blob/main/reference/verb/build.rst)
5. [Packages and Workspaces, Northwestern MSR ROS notes](https://nu-msr.github.io/ros_notes/ros2/colcon.html)
6. [colcon-bundle: A colcon extension to create portable application bundles, GitHub](https://github.com/colcon/colcon-bundle)
7. [Building and Bundling a ROS Application for AWS RoboMaker, AWS Open Source Blog](https://aws.amazon.com/blogs/opensource/building-bundling-ros-app-aws-robomaker/)
8. [Isolated vs Merged Workspaces, colcon documentation](https://colcon.readthedocs.io/en/released/user/isolated-vs-merged-workspaces.html)
9. [Colcon Tutorial, ROS 2 documentation](https://github.com/ros2/ros2_documentation/blob/rolling/source/ROS-Framework/client-libraries/Working-with-Client-Libraries/Colcon-Tutorial.rst)
10. [test / test-result Command, ROSCon 2019 colcon presentation](https://roscon.ros.org/2019/talks/roscon2019_colcon.pdf)
11. [test-result - Summarize Test Results, colcon documentation](https://colcon.readthedocs.io/en/released/reference/verb/test-result.html)
12. [Extension points, colcon documentation](https://colcon.readthedocs.io/en/released/developer/extension-point.html)
13. [colcon-cargo setup.cfg, GitHub](https://github.com/colcon/colcon-cargo/blob/main/setup.cfg)
14. [colcon-core bin/colcon bootstrap script, GitHub](https://github.com/colcon/colcon-core/blob/master/bin/colcon)
15. [Proposal: Migration of colcon-core to PEP 517/518/621/660 Standards, colcon-core issue #736](https://github.com/colcon/colcon-core/issues/736)
16. [Uv on ROS 2: a field report on workspace-level virtual environments, Open Robotics Discourse](https://discourse.openrobotics.org/t/uv-on-ros-2-a-field-report-on-workspace-level-virtual-environments-five-failure-modes-and-minimal-colcon-ament-proposals/57111)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Build, deployment, and development tooling › Build systems and build tools

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/colcon

English original: https://www.edgepedia.org/colcon
