colcon编译

安装 colcon : sudo apt install python3-colcon-common-extensions。 ROS2的build没有了ROS1中的devel概念

与ROS1不同,如果一个包不放在src目录,而放在工作空间目录,colcon也会编译它。

  • colcon build     编译所有包

  • colcon build —packages-select pkg     只编译一个包

colcon build --packages-select 指令并不会编译该包的依赖,往往会报错。可以用下面这条指令进行包和其依赖编译

1
colcon build --packages-up-to   package_name
  • colcon build —cmake-args -DCMAKE_BUILD_TYPE=Release    

不编译测试单元

colcon test --packages-select YOUR_PKG_NAME --cmake-args -DBUILD_TESTING = 0

运行编译的包的测试

colcon test

  • 允许通过更改src下的部分文件改变install,这样每次修改Python脚本时不必重新 build

colcon build --symlink-install

  • colcon build —symlink-install pkg   

  • colcon build —symlink-install —packages-ignore pkg    

减小colcon的CPU占用

指定并行的线程数

1
colcon build --symlink-install --parallel-workers 1

不使用并行编译

1
colcon build --symlink-install  --executor sequential

两种编译不要混合用,否则每次都会重新编译

显示CMake中的MESSAGE

colcon build —event-handlers console_direct+ —packages-select test_node

console_direct 换成 console_cohesion 也可以

如果不想编译特定的包,在该包目录里面创建一个名为COLCON_IGNORE文件,这样子这个包就不会被索引到了

问题

  1. 编译时报错 CMake Error at /opt/ros/jazzy/share/rosidl_typesupport_c/cmake/get_used_typesupports.cmake:35 (message):
    No ‘rosidl_typesupport_c’ found

执行source后再编译

  1. 截图 2025-07-16 09-49-13.png

一开始我以为是/opt里的安装有问题,后来发现是ament_target_dependencies里没有添加nav_msgs,导致include头文件时会找不到

  1. failed to create symbolic link ‘/home/xxx/learning_interfaces2’ because existing path cannot be removed: Is a directory

把对应的目录删掉

  1. CMake Warning: Manually-specified variables were not used by the project: CATKIN_INSTALL_INTO_PREFIX_ROOT

在相应的包里加入

1
2
3
<export>                               
<build_type>ament_cmake</build_type>
</export>

  1. 如果需要在包的package.xml中添加依赖关系。比如sample包依赖common包,那么就在sample包的package.xml中添加
1
<depend>common</depend>

    告诉编译器,编译B包的时候依赖A包,A包需要先编译出来。

  1. 没有那个文件或目录
1
2
3
4
Starting >>> test_node
--- stderr: test_node
cc1plus: fatal error: /home/user/catkin_ws/src/test_node/src/Action.cpp: 没有那个文件或目录
compilation terminated.

删掉build目录中的test_node文件夹

  1. 编译自定义的msg包时,报错 找不到rosidl_typesupport_c

一般都安装了相应的库:

1
sudo apt install ros-jazzy-rosidl-typesupport-c

source /opt/ros/jazzy/setup.bash 即可

  1. CMake Warning: Manually-specified variables were not used by the project: CATKIN_INSTALL_INTO_PREFIX_ROOT

相应的包里明明没有用CATKIN相关的东西,不知是哪里报警,只好在CMake里加一句 unset( CATKIN_INSTALL_INTO_PREFIX_ROOT )

  1. 编译时找不到 ament_cmake

source /opt/ros/jazzy/setup.bash

  1. 报警: WARNING:colcon.colcon_ros.prefix_path.catkin:The path ‘/home/user/catkin_ws/install/nav2_map_server’ in the environment variable CMAKE_PREFIX_PATH doesn’t exist

即使删除了install, build, log也无法解决,要想彻底解决只能重建一个工作空间。或者用临时方法,但新终端又会失效

1
2
unset AMENT_PREFIX_PATH
unset CMAKE_PREFIX_PATH

  1. 包的依赖问题

在路径 /build/my_package/ament_cmake_core 可以看到编译一个包后生成的cmake文件,比如my_packageConfig.cmakemy_packageConfig-version.cmake

如果包A需要包B,编译时报错找不到BConfig.cmake,在工作空间执行 source