小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
第三章 自定义消息msg 编译错误
-
CMakeLists.txt 文件如下
cmake_minimum_required(VERSION 3.8)
project(status_interfaces)if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()find_package(ament_cmake REQUIRED)
find_package(builtin_interfaces REQUIRED)
find_package(rosidl_default_generators REQUIRED)rosidl_generate_interfaces (${PROJECT_NAME}
"msg/SystemStatus.msg"
DEPENDENCIES builtin_interfaces
)if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
set(ament_cmake_copyright_FOUND TRUE)
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()ament_package()
package.xml文件如下
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>status_interfaces</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="lsy@todo.todo">lsy</maintainer>
<license>Apache-2.0</license><member_of_group>rosidl_interface_packages </member_of_group>
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>builtin_interfaces</depend>
<depend>rosidl_default_generators</depend><test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend><export>
<build_type>ament_cmake</build_type>
</export>
</package>报错信息如下
CMake Error at /opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:240 (list):
list index: 1 out of range (-1, 0)
Call Stack (most recent call first):
CMakeLists.txt:14 (rosidl_generate_interfaces)CMake Error at /opt/ros/humble/share/rosidl_generator_c/cmake/rosidl_generator_c_generate_interfaces.cmake:69 (message):
Target dependency
'/chapt3/topic_practice_ws/build/status_interfaces/rosidl_adapter/status_interfaces/msg/SystemStatus.idl'
does not exist
Call Stack (most recent call first):
/opt/ros/humble/share/ament_cmake_core/cmake/core/ament_execute_extensions.cmake:48 (include)
/opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:286 (ament_execute_extensions)
CMakeLists.txt:14 (rosidl_generate_interfaces) -
这个错误通常发生在CMake配置过程中,特别是在使用
rosidl_generate_interfaces
时,它负责根据.msg
或.srv
文件生成相应的ROS接口代码。错误提示表明在尝试访问列表索引时出现了范围错误,以及生成的IDL文件不存在。这里有几个可能的原因和解决方案:1. 检查
.msg
文件路径确保
SystemStatus.msg
文件的路径正确无误,并且CMake能够正确找到它。在你的CMakeLists.txt
中,你指定了"msg/SystemStatus.msg"
,这意味着CMake将在当前CMakeLists.txt文件所在的目录下的msg
子目录中查找这个文件。请检查这个路径是否正确。2. 确保
.msg
文件存在且格式正确检查
SystemStatus.msg
文件是否确实存在于指定的路径中,并且其内容格式符合ROS的.msg
文件规范。例如,一个简单的.msg
文件可能看起来像这样:# SystemStatus.msg Header header bool is_active
3. 检查CMakeLists.txt中的
rosidl_generate_interfaces
调用确保
rosidl_generate_interfaces
函数的调用正确无误。你已经正确地指定了依赖项builtin_interfaces
,但请确保没有其他语法错误或遗漏。4. 清理并重新构建
有时候,旧的构建文件或缓存可能会导致问题。尝试清理你的构建目录(通常是
build
或devel
文件夹),然后重新运行CMake配置和构建过程。# 在你的工作空间中 rm -rf build/ devel/ mkdir build cd build cmake .. make
5. 检查ROS环境
确保你的ROS环境已经正确设置,并且所有的依赖包都已正确安装。你可以通过运行
rosdep install --from-paths src --ignore-src -r -y
来安装所有缺失的依赖。6. 查看CMake的详细输出
运行CMake时,可以添加
-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON
选项来获取更详细的输出,这有助于诊断问题。7. 检查ROS版本和CMake版本
确保你使用的ROS版本和CMake版本与你的项目和依赖包兼容。
8. 检查
package.xml
虽然
package.xml
文件与这个具体的CMake错误不直接相关,但确保它的内容正确且格式良好也是很重要的。你已经提供了一个看起来正确的package.xml
,但请确保所有标签都正确关闭,并且没有不可见的特殊字符或编码问题。通过以上步骤,你应该能够诊断并解决这个CMake错误。如果问题仍然存在,请检查是否有任何特定的ROS社区或论坛可以提供更具体的帮助。
以上为生成回答,仅供参考~
-
跑github上小鱼的例程也是同样的报错
Starting >>> status_interfaces
--- stderr: status_interfaces
CMake Error at /opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:240 (list):
list index: 1 out of range (-1, 0)
Call Stack (most recent call first):
CMakeLists.txt:13 (rosidl_generate_interfaces)CMake Error at /opt/ros/humble/share/rosidl_generator_c/cmake/rosidl_generator_c_generate_interfaces.cmake:69 (message):
Target dependency
'/ros2bookcode-master/chapt3/topic_practice_ws/build/status_interfaces/rosidl_adapter/status_interfaces/msg/SystemStatus.idl'
does not exist
Call Stack (most recent call first):
/opt/ros/humble/share/ament_cmake_core/cmake/core/ament_execute_extensions.cmake:48 (include)
/opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:286 (ament_execute_extensions)
CMakeLists.txt:13 (rosidl_generate_interfaces)
Failed <<< status_interfaces [0.24s, exited with code 1]
Summary: 0 packages finished [0.36s]
1 package failed: status_interfaces
1 package had stderr output: status_interfaces
2 packages not processed -
@liusiyu_alex_nuaa 我也遇到同样的问题
-
@2352947187 我解决了,是因为该文件的路径中含有中文,把整个工作空间换个路径,然后把除了src的其他文件删除,再colcon build就可以了
-
@liusiyu_alex_nuaa 谢了兄弟,我搞了一天