colcon build失败 提示:CMake Error: The source directory "/opt/ros/humble/share/gtest_vendor" does not appear to contain CMakeLists.txt.
-
root@cq-virtual-machine:/opt/ros/humble# colcon build
Starting >>> ament_package
Starting >>> ament_lint
Starting >>> gtest_vendor
Starting >>> gtest
Starting >>> ament_pycodestyle
Starting >>> ament_cppcheck
Starting >>> osrf_pycommon
Starting >>> google_benchmark_vendor
[2.564s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'ament_package' doesn't explicitly install a marker in the package index (colcon-ros currently does it implicitly but that fallback will be removed in the future)
[2.564s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'ament_package' doesn't explicitly install the 'package.xml' file (colcon-ros currently does it implicitly but that fallback will be removed in the future)
[2.568s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'ament_lint' doesn't explicitly install a marker in the package index (colcon-ros currently does it implicitly but that fallback will be removed in the future)
[2.568s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'ament_lint' doesn't explicitly install the 'package.xml' file (colcon-ros currently does it implicitly but that fallback will be removed in the future)
[2.586s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'ament_pycodestyle' doesn't explicitly install a marker in the package index (colcon-ros currently does it implicitly but that fallback will be removed in the future)
[2.586s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'ament_pycodestyle' doesn't explicitly install the 'package.xml' file (colcon-ros currently does it implicitly but that fallback will be removed in the future)
[2.593s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'ament_cppcheck' doesn't explicitly install a marker in the package index (colcon-ros currently does it implicitly but that fallback will be removed in the future)
[2.595s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'ament_cppcheck' doesn't explicitly install the 'package.xml' file (colcon-ros currently does it implicitly but that fallback will be removed in the future)
[2.599s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'osrf_pycommon' doesn't explicitly install a marker in the package index (colcon-ros currently does it implicitly but that fallback will be removed in the future)
[2.599s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'osrf_pycommon' doesn't explicitly install the 'package.xml' file (colcon-ros currently does it implicitly but that fallback will be removed in the future)
--- stderr: gtest_vendor
CMake Error: The source directory "/opt/ros/humble/share/gtest_vendor" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.Failed <<< gtest_vendor [0.12s, exited with code 1]
Aborted <<< google_benchmark_vendor [0.10s]
Aborted <<< ament_package [0.14s]
Aborted <<< ament_cppcheck [0.11s]
Aborted <<< ament_lint [0.13s]
Aborted <<< ament_pycodestyle [0.12s]
Aborted <<< osrf_pycommon [0.11s]
Aborted <<< gtest [0.34s]Summary: 0 packages finished [2.78s]
1 package failed: gtest_vendor
7 packages aborted: ament_cppcheck ament_lint ament_package ament_pycodestyle google_benchmark_vendor gtest osrf_pycommon
2 packages had stderr output: google_benchmark_vendor gtest_vendor
290 packages not processed -
@qic 在 colcon build失败 提示:CMake Error: The source directory "/opt/ros/humble/share/gtest_vendor" does not appear to contain CMakeLists.txt. 中说:
root@cq-virtual-machine:/opt/ros/humble# colcon build
Starting >>> ament_package
Starting >>> ament_lint
Starting >>> gtest_vendor
Starting >>> gtest
Starting >>> ament_pycodestyle
Starting >>> ament_cppcheck
Starting >>> osrf_pycommon
Starting >>> google_benchmark_vendor
[2.564s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'ament_package' doesn't explicitly install a marker in the package index (colcon-ros currently does it implicitly but that fallback will be removed in the future)
[2.564s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'ament_package' doesn't explicitly install the 'package.xml' file (colcon-ros currently does it implicitly but that fallback will be removed in the future)
[2.568s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'ament_lint' doesn't explicitly install a marker in the package index (colcon-ros currently does it implicitly but that fallback will be removed in the future)
[2.568s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'ament_lint' doesn't explicitly install the 'package.xml' file (colcon-ros currently does it implicitly but that fallback will be removed in the future)
[2.586s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'ament_pycodestyle' doesn't explicitly install a marker in the package index (colcon-ros currently does it implicitly but that fallback will be removed in the future)
[2.586s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'ament_pycodestyle' doesn't explicitly install the 'package.xml' file (colcon-ros currently does it implicitly but that fallback will be removed in the future)
[2.593s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'ament_cppcheck' doesn't explicitly install a marker in the package index (colcon-ros currently does it implicitly but that fallback will be removed in the future)
[2.595s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'ament_cppcheck' doesn't explicitly install the 'package.xml' file (colcon-ros currently does it implicitly but that fallback will be removed in the future)
[2.599s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'osrf_pycommon' doesn't explicitly install a marker in the package index (colcon-ros currently does it implicitly but that fallback will be removed in the future)
[2.599s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'osrf_pycommon' doesn't explicitly install the 'package.xml' file (colcon-ros currently does it implicitly but that fallback will be removed in the future)
--- stderr: gtest_vendor
CMake Error: The source directory "/opt/ros/humble/share/gtest_vendor" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
Failed <<< gtest_vendor [0.12s, exited with code 1]
Aborted <<< google_benchmark_vendor [0.10s]
Aborted <<< ament_package [0.14s]
Aborted <<< ament_cppcheck [0.11s]
Aborted <<< ament_lint [0.13s]
Aborted <<< ament_pycodestyle [0.12s]
Aborted <<< osrf_pycommon [0.11s]
Aborted <<< gtest [0.34s]Summary: 0 packages finished [2.78s]
1 package failed: gtest_vendor
7 packages aborted: ament_cppcheck ament_lint ament_package ament_pycodestyle google_benchmark_vendor gtest osrf_pycommon
2 packages had stderr output: google_benchmark_vendor gtest_vendor
290 packages not processed从报错信息来看,你在 /opt/ros/humble 目录下直接执行 colcon build 导致了错误。这是典型的操作误区,具体原因和解决方法如下:
核心问题
/opt/ros/humble 是 ROS 2 系统级安装目录(通过 apt 安装的二进制包存放路径),而非你的工作空间。该目录下的包是预编译好的二进制文件,不包含源代码和 CMakeLists.txt,因此无法直接用 colcon build 编译。
正确操作步骤- 退出系统目录,创建并使用自己的工作空间
ROS 2 开发应在 用户自定义的工作空间 中进行,而非系统安装目录:
bash
回到用户目录
cd ~
创建并进入工作空间(例如 ros2_ws)
mkdir -p ros2_ws/src
cd ros2_ws仅在自己的工作空间中执行编译(此时src目录为空,会提示无包可编译,属于正常现象)
colcon build
2. 若需修改源码,需先下载对应包的源代码到工作空间
如果需要修改 gtest_vendor 等包的源码,需从 GitHub 克隆源码到工作空间的 src 目录,再编译:
bash进入工作空间的src目录
cd ~/ros2_ws/src
克隆需要修改的包(以gtest_vendor为例)
git clone https://github.com/ros2/gtest_vendor.git -b humble # 注意切换到humble分支
回到工作空间根目录编译
cd ~/ros2_ws
colcon build --packages-select gtest_vendor # 仅编译该包
3. 理解系统目录与工作空间的区别
/opt/ros/humble:系统级安装目录,存放通过 apt install 安装的二进制包,不可直接编译,也不建议修改(需要 root 权限,且易被系统更新覆盖)。
用户工作空间(如 ~/ros2_ws):存放自定义代码或需要修改的源码,通过 colcon build 编译,编译产物在 install 目录,不会影响系统级安装。
总结
你需要 在自己的工作空间中进行编译,而非系统安装目录。若仅需使用 ROS 2 功能,无需编译系统目录下的包;若需修改源码,需将对应包的源代码克隆到工作空间后再编译。
按上述步骤创建工作空间并正确放置源码后,colcon build 即可正常执行。
这种错误输出自己看不懂直接发给AI比在这里快 - 退出系统目录,创建并使用自己的工作空间
-
@凄凉的泪水 好的,感谢大佬