ROS2自定义接口报错
-
报错如下:
linux@linux-HP-246-14-inch-G10-Notebook-PC:~/status_pose$ colcon build
Starting >>> status_pose
--- stderr: status_pose
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
Compatibility with CMake < 3.10 will be removed from a future version of
CMake.Update the VERSION argument <min> value. Or, use the <min>...<max> syntax
to tell CMake that the project requires at least <min> but has been updated
to work with policies introduced by <max> or earlier.CMake Error at /opt/ros/humble/share/rosidl_generator_py/cmake/rosidl_generator_py_generate_interfaces.cmake:20 (find_package):
By not providing "FindPythonInterp.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"PythonInterp", but CMake did not find one.Could not find a package configuration file provided by "PythonInterp"
(requested version 3.6) with any of the following names:PythonInterpConfig.cmake pythoninterp-config.cmake
Add the installation prefix of "PythonInterp" to CMAKE_PREFIX_PATH or set
"PythonInterp_DIR" to a directory containing one of the above files. If
"PythonInterp" provides a separate development package or SDK, be sure it
has been installed.
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:26 (rosidl_generate_interfaces)
Failed <<< status_pose [2.17s, exited with code 1]
Summary: 0 packages finished [2.40s]
1 package failed: status_pose
1 package had stderr output: status_pose
linux@linux-HP-246-14-inch-G10-Notebook-PC:~/status_pose$
Cmakelist如下:
cmake_minimum_required(VERSION 3.5) # 使用至少 CMake 3.5 版本
project(status_pose)设置政策以避免使用已弃用的模块
if(POLICY CMP0094)
cmake_policy(SET CMP0094 NEW)
endif()if(POLICY CMP0148)
cmake_policy(SET CMP0148 NEW)
endif()set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/Modules)
find dependencies
find_package(ament_cmake REQUIRED)
find_package(builtin_interfaces REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(rclcpp REQUIRED) # 添加对rclcpp的支持
find_package(Python3 REQUIRED COMPONENTS Interpreter Development) # 使用 FindPython3 模块
find_package(rosidl_typesupport_cpp REQUIRED) # 添加这一行
find_package(rosidl_typesupport_fastrtps_cpp REQUIRED) # 添加这一行
find_package(rosidl_typesupport_introspection_cpp REQUIRED) # 添加这一行Generate interfaces
rosidl_generate_interfaces(${PROJECT_NAME}
"msg/SystemStatus.msg"
DEPENDENCIES builtin_interfaces
)Include directories for generated headers
include_directories(${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp/${PROJECT_NAME})
include_directories(${CMAKE_CURRENT_BINARY_DIR}/rosidl_typesupport_fastrtps_cpp/${PROJECT_NAME})Define executable
add_executable(SystemStatusPublisher src/SystemStatusPublisher.cpp)
Ensure the generated message headers are included in the include directories
target_include_directories(SystemStatusPublisher PRIVATE
${rosidl_generator_cpp_INCLUDE_DIRS}
${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp/${PROJECT_NAME}
${CMAKE_CURRENT_BINARY_DIR}/rosidl_typesupport_fastrtps_cpp/${PROJECT_NAME}
)Link dependencies to the target using ament_target_dependencies
ament_target_dependencies(SystemStatusPublisher
rclcpp
builtin_interfaces
rosidl_default_runtime
rosidl_typesupport_cpp
rosidl_typesupport_fastrtps_cpp
rosidl_typesupport_introspection_cpp
)Install the executable
install(TARGETS SystemStatusPublisher
DESTINATION lib/${PROJECT_NAME})Export runtime dependencies
ament_export_dependencies(rosidl_default_runtime)
ament_export_dependencies(rosidl_typesupport_cpp)
ament_export_dependencies(rosidl_typesupport_fastrtps_cpp)
ament_export_dependencies(rosidl_typesupport_introspection_cpp)Install message files
install(
DIRECTORY msg/
DESTINATION share/${PROJECT_NAME}/
)if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)the following line skips the linter which checks for copyrights
comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
the following line skips cpplint (only works in a git repo)
comment the line when this package is in a git repo and when
a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package() -
根据您提供的错误信息和
CMakeLists.txt
文件,问题主要出在 CMake 试图找到 Python 解释器的配置,但使用了find_package(Python3 ...)
,这在 ROS 2 中通常不是正确的方法来查找和配置 Python。ROS 2 使用find_package
来查找 ROS 2 相关的包,而不是用于配置 Python 解释器。在 ROS 2 中,Python 的配置通常通过
setup.py
或pyproject.toml
(对于 Python 包)以及 CMake 的FindPython3
模块来处理。这里有几个步骤可以帮助您解决问题:-
更新 CMake 最低版本要求:
ROS 2 Humble 推荐使用更高版本的 CMake。您的CMakeLists.txt
中设置的是 3.5,建议更新为更高的版本,如 3.10 或更高,以避免将来的兼容性问题。cmake_minimum_required(VERSION 3.10)
-
修改 Python 的查找方式:
在 ROS 2 中,通常不需要直接调用find_package
来查找 Python 解释器。相反,您应该使用 CMake 的FindPython3
模块来配置 Python。在CMakeLists.txt
中,您可以添加以下内容来查找 Python:# 查找 Python3 find_package(Python3 COMPONENTS Interpreter REQUIRED)
但是,更常见的是使用
ament_python
来处理 Python 包的构建和依赖。如果您正在构建一个 Python 包,您应该考虑使用setup.py
或pyproject.toml
。 -
检查 Python 配置:
确保您的系统上安装了正确版本的 Python,并且 CMake 能够找到它。您可以通过运行python3 --version
来检查 Python 版本。 -
移除或修改非必要的
find_package
调用:
如果您的包不是用 Python 编写的,并且不需要Python3
组件,那么应该移除或修改这些调用。 -
清理并重新构建:
在修改CMakeLists.txt
后,确保清理旧的构建文件并重新构建您的包:colcon build --packages-clear status_pose colcon build
-
检查 ROS 2 环境:
确保您的 ROS 2 环境已正确设置,包括所有必要的源路径和设置脚本。
如果您正在构建一个包含 Python 和 C++ 组件的混合包,您可能需要进一步调整您的构建系统,以正确地处理两种语言的依赖和构建。在这种情况下,考虑使用
ament_python
和适当的 CMake 配置来确保一切都能正确构建和链接。
以上为生成回答,仅供参考~
-
-
@小伊 新问题出现了,我打算构建纯C++功能包,所以C++程序如下:
#include <memory>
#include <chrono>
#include <functional>#include "rclcpp/rclcpp.hpp"
#include "builtin_interfaces/msg/time.hpp"
#include "status_pose/msg/system_status.hpp"class SystemStatusPublisher : public rclcpp::Node
{
public:
SystemStatusPublisher()
: Node("system_status_publisher"), count_(0)
{
publisher_ = this->create_publisher<status_pose::msg::SystemStatus>("system_status", 10);
timer_ = this->create_wall_timer(
std::chrono::seconds(1),
std::bind(&SystemStatusPublisher::timer_callback, this));
}private:
void timer_callback()
{
auto message = status_pose::msg::SystemStatus();
message.cpu_percent = 10.0;
message.memory_percent = 20.0;
message.host_name = "linux-HP-246-14-inch-G10-Notebook-PC";
message.memory_available = 1024.0;
message.net_recv = 100.0;
message.net_sent = 200.0;
message.timestamp = this->now();RCLCPP_INFO(this->get_logger(), "Publishing: 'cpu_percent: %f'", message.cpu_percent); publisher_->publish(message);
}
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher<status_pose::msg::SystemStatus>::SharedPtr publisher_;
size_t count_;
};int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<SystemStatusPublisher>());
rclcpp::shutdown();
return 0;
}
Cmakelist如下:
cmake_minimum_required(VERSION 3.10) # 使用至少 CMake 3.10 版本
project(status_pose)设置政策以避免使用已弃用的模块
if(POLICY CMP0094)
cmake_policy(SET CMP0094 NEW)
endif()if(POLICY CMP0148)
cmake_policy(SET CMP0148 NEW)
endif()查找依赖项
find_package(ament_cmake REQUIRED)
find_package(builtin_interfaces REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(rclcpp REQUIRED) # 添加对rclcpp的支持
find_package(rosidl_typesupport_fastrtps_cpp REQUIRED) # 添加这一行生成接口(仅限 C++)
rosidl_generate_interfaces(${PROJECT_NAME}
"msg/SystemStatus.msg"
DEPENDENCIES builtin_interfaces
LANGUAGE cpp
)包含生成头文件的目录
include_directories(${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp/${PROJECT_NAME})
include_directories(${CMAKE_CURRENT_BINARY_DIR}/rosidl_typesupport_fastrtps_cpp/${PROJECT_NAME})定义可执行文件
add_executable(SystemStatusPublisher src/SystemStatusPublisher.cpp)
确保生成的消息头文件包含在包含目录中
target_include_directories(SystemStatusPublisher PRIVATE
${rosidl_generator_cpp_INCLUDE_DIRS}
${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp/${PROJECT_NAME}
${CMAKE_CURRENT_BINARY_DIR}/rosidl_typesupport_fastrtps_cpp/${PROJECT_NAME}
)链接依赖项到目标
ament_target_dependencies(SystemStatusPublisher
rclcpp
builtin_interfaces
rosidl_default_runtime
rosidl_typesupport_fastrtps_cpp
)安装可执行文件
install(TARGETS SystemStatusPublisher
DESTINATION lib/${PROJECT_NAME})导出运行时依赖项
ament_export_dependencies(rosidl_default_runtime)
ament_export_dependencies(rosidl_typesupport_fastrtps_cpp)安装消息文件
install(
DIRECTORY msg/
DESTINATION share/${PROJECT_NAME}/
)if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)下面这行跳过了检查版权声明
在所有源文件添加版权声明和许可证后取消注释
set(ament_cmake_copyright_FOUND TRUE)
下面这行跳过了 cpplint(仅适用于 Git 仓库)
在所有源文件添加版权声明和许可证后取消注释
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_pose</name>
<version>0.0.0</version>
<description>ROS 2 package for publishing system status messages.</description>
<maintainer email="2968436050@qq.com">linux</maintainer>
<license>Apache-2.0</license>
<member_of_group>rosidl_interface_packages</member_of_group>
<buildtool_depend>ament_cmake</buildtool_depend>
<build_depend>rclcpp</build_depend>
<build_depend>builtin_interfaces</build_depend>
<build_depend>rosidl_default_generators</build_depend>
<build_depend>rosidl_typesupport_cpp</build_depend>
<exec_depend>rclcpp</exec_depend>
<exec_depend>builtin_interfaces</exec_depend>
<exec_depend>rosidl_default_runtime</exec_depend>
<exec_depend>rosidl_typesupport_cpp</exec_depend>
<exec_depend>rosidl_typesupport_fastrtps_cpp</exec_depend>
<exec_depend>rosidl_typesupport_introspection_cpp</exec_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
使用了vscode,配置文件如下:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/opt/ros/humble/include",
"/usr/include/c++/11",
"/usr/include/x86_64-linux-gnu/c++/11",
"/usr/include/c++/11/backward",
"/usr/lib/gcc/x86_64-linux-gnu/11/include",
"/usr/local/include",
"/usr/include/x86_64-linux-gnu",
"/usr/include",
"/opt/ros/humble/include/rclcpp",
"/opt/ros/humble/include/builtin_interfaces",
"/opt/ros/humble/include/rosidl_typesupport_cpp",
"/opt/ros/humble/include/rosidl_runtime_cpp",
"/opt/ros/humble/include/rosidl_typesupport_interface",
"/opt/ros/humble/include/rcutils",
"/opt/ros/humble/include/rosidl_typesupport_fastrtps_cpp",
"/opt/ros/humble/include/rosidl_typesupport_introspection_cpp"
],
"defines": [],
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
最后报错信息如下:
linux@linux-HP-246-14-inch-G10-Notebook-PC:~/status_pose$ colcon build --cmake-clean-cache
Starting >>> status_pose
--- stderr: status_pose
CMake Error at /opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:162 (message):
rosidl_generate_interfaces() the passed dependency 'LANGUAGE' has not been
found before using find_package()
Call Stack (most recent call first):
CMakeLists.txt:21 (rosidl_generate_interfaces)
Failed <<< status_pose [1.59s, exited with code 1]
Summary: 0 packages finished [1.79s]
1 package failed: status_pose
1 package had stderr output: status_pose -
从你提供的错误信息和CMakeLists.txt文件来看,问题出在
rosidl_generate_interfaces
函数的调用上。错误信息指出:“the passed dependency 'LANGUAGE' has not been found before using find_package()”。这通常意味着在调用rosidl_generate_interfaces
时,参数的使用方式不正确。在ROS 2中,
rosidl_generate_interfaces
函数用于生成消息、服务和类型的接口代码。这个函数通常的调用格式如下:rosidl_generate_interfaces( <PROJECT_NAME> FILES <list_of_interface_files> DEPENDENCIES <list_of_dependencies> TARGET_DEPENDENCIES <list_of_target_dependencies> # (可选) LANGUAGES <list_of_languages> # 注意这里是LANGUAGES,不是LANGUAGE )
在你的CMakeLists.txt中,你使用了
LANGUAGE cpp
,但正确的应该是LANGUAGES cpp
。修改后的相关部分应该如下:rosidl_generate_interfaces(${PROJECT_NAME} "msg/SystemStatus.msg" DEPENDENCIES builtin_interfaces LANGUAGES cpp )
此外,还有一些其他的注意事项和可能的改进:
-
CMake Policy 设置:你的CMakeLists.txt中设置了两个CMake政策,但没有检查这些政策是否适用于你的CMake版本。通常,这些设置是为了解决跨版本兼容性问题,确保你的CMake版本与这些政策兼容。
-
安装消息文件:你的
install
指令应该确保消息文件被正确安装到ROS 2的标准路径下,通常是在share/<package_name>/msg/
。你的设置看起来是正确的,但确保msg/
目录确实包含了你定义的.msg
文件。 -
包依赖:在
package.xml
中,你列出了很多依赖,包括rosidl_typesupport_cpp
和rosidl_typesupport_introspection_cpp
,但在CMakeLists.txt中并没有看到对这些类型的支持进行明确的配置。如果你不需要这些类型的支持,可以从package.xml
中移除它们。如果你需要,确保在CMakeLists.txt中添加了相应的配置。 -
VSCode 配置:你的VSCode配置看起来相当完整,确保所有路径都是正确的,特别是ROS 2的安装路径和相关的include路径。
在修改CMakeLists.txt后,重新运行
colcon build --cmake-clean-cache
来清理旧的构建缓存并重新构建你的包。这应该解决你遇到的问题。如果还有其他问题,可能需要检查其他配置或依赖是否正确。
以上为生成回答,仅供参考~
-
-
@小伊linux@linux-HP-246-14-inch-G10-Notebook-PC:~/status_pose$ colcon build --cmake-clean-cache
Starting >>> status_pose
--- stderr: status_pose
CMake Error at /opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:162 (message):
rosidl_generate_interfaces() the passed dependency 'LANGUAGES' has not been
found before using find_package()
Call Stack (most recent call first):
CMakeLists.txt:12 (rosidl_generate_interfaces)
Failed <<< status_pose [1.57s, exited with code 1]
Summary: 0 packages finished [1.73s]
1 package failed: status_pose
1 package had stderr output: status_pose -
这个错误信息表明在使用
colcon build
命令构建status_pose
包时遇到了问题。错误发生在 CMake 的配置阶段,具体是在rosidl_generate_interfaces.cmake
文件中。错误消息指出rosidl_generate_interfaces()
函数中传递的依赖LANGUAGES
在调用find_package()
之前没有被找到。这里有几个可能的解决步骤:
-
检查 CMakeLists.txt 文件:
打开status_pose
包的CMakeLists.txt
文件,检查第 12 行附近的rosidl_generate_interfaces
调用。确保在调用这个函数之前已经正确设置了所有必要的依赖和变量,特别是LANGUAGES
。通常,LANGUAGES
应该是一个列表,比如set(LANGUAGES "cpp" "py")
,用来指定需要生成的接口语言。 -
确保所有依赖都已正确安装:
确保你的环境中已经安装了所有必要的 ROS 2 组件和依赖。你可以检查你的 ROS 2 安装是否完整,特别是与rosidl
相关的部分。 -
清理并重新构建:
使用--cmake-clean-cache
选项已经是一个很好的开始,因为它会清理旧的 CMake 缓存。但有时候,完全删除构建目录(通常是build
或install
目录)并重新运行colcon build
可以解决问题。 -
检查 ROS 2 版本兼容性:
确保你的status_pose
包是为当前安装的 ROS 2 版本(在这个案例中是 Humble)开发的。有时候,包的某些部分可能依赖于特定版本的 ROS 2,而在其他版本中可能不兼容。 -
查看文档和社区资源:
如果上述步骤都无法解决问题,查看 ROS 2 的官方文档或搜索相关的社区论坛和问答网站可能会有帮助。可能有其他开发者遇到过类似的问题,并分享了解决方案。
通过上述步骤,你应该能够定位问题的原因,并找到解决这个问题的方法。如果问题仍然存在,可能需要更详细地检查你的项目配置或寻求来自 ROS 2 社区的帮助。
以上为生成回答,仅供参考~
-