小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
自定义接口编译时报错
-
环境:
win11家庭版
humble问题:
按照https://fishros.com/d2lros2/#/humble/chapt3/get_started/7.ROS2%E6%8E%A5%E5%8F%A3%E4%BB%8B%E7%BB%8D?id=_33-%e5%88%9b%e5%bb%ba%e6%8e%a5%e5%8f%a3%e5%8a%9f%e8%83%bd%e5%8c%85%e7%bc%96%e6%8e%a5%e5%8f%a3的教程建立自定义接口,包名叫demo_interfaces,接口名同小鱼,但是在编译失败,提示信息如下:
Starting >>> demo_interfaces
--- stderr: demo_interfaces
CMake Warning (dev) at D:/ROS2/ros2-windows/share/rosidl_generator_py/cmake/rosidl_generator_py_generate_interfaces.cmake:20 (find_package):
Policy CMP0148 is not set: The FindPythonInterp and FindPythonLibs modules
are removed. Run "cmake --help-policy CMP0148" for policy details. Use
the cmake_policy command to set the policy and suppress this warning.Call Stack (most recent call first):
D:/ROS2/ros2-windows/share/ament_cmake_core/cmake/core/ament_execute_extensions.cmake:48 (include)
D:/ROS2/ros2-windows/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:286 (ament_execute_extensions)
CMakeLists.txt:25 (rosidl_generate_interfaces)
This warning is for project developers. Use -Wno-dev to suppress it.CMake Warning (dev) at D:/ROS2/ros2-windows/share/python_cmake_module/cmake/Modules/FindPythonExtra.cmake:52 (find_package):
Policy CMP0148 is not set: The FindPythonInterp and FindPythonLibs modules
are removed. Run "cmake --help-policy CMP0148" for policy details. Use
the cmake_policy command to set the policy and suppress this warning.Call Stack (most recent call first):
D:/ROS2/ros2-windows/share/rosidl_generator_py/cmake/rosidl_generator_py_generate_interfaces.cmake:23 (find_package)
D:/ROS2/ros2-windows/share/ament_cmake_core/cmake/core/ament_execute_extensions.cmake:48 (include)
D:/ROS2/ros2-windows/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:286 (ament_execute_extensions)
CMakeLists.txt:25 (rosidl_generate_interfaces)
This warning is for project developers. Use -Wno-dev to suppress it.CMake Warning (dev) at D:/ROS2/ros2-windows/share/python_cmake_module/cmake/Modules/FindPythonExtra.cmake:140 (find_package):
Policy CMP0148 is not set: The FindPythonInterp and FindPythonLibs modules
are removed. Run "cmake --help-policy CMP0148" for policy details. Use
the cmake_policy command to set the policy and suppress this warning.Call Stack (most recent call first):
D:/ROS2/ros2-windows/share/rosidl_generator_py/cmake/rosidl_generator_py_generate_interfaces.cmake:23 (find_package)
D:/ROS2/ros2-windows/share/ament_cmake_core/cmake/core/ament_execute_extensions.cmake:48 (include)
D:/ROS2/ros2-windows/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:286 (ament_execute_extensions)
CMakeLists.txt:25 (rosidl_generate_interfaces)
This warning is for project developers. Use -Wno-dev to suppress it.===========
关键点来了:
我把cmakelist中“find_package(rosidl_default_generators REQUIRED)”这一行注释掉以后就可以编译过了,但是查看pkg list是可以找到这个包的呀,所以,问题出现在哪里??? -
您遇到的问题与CMake在寻找Python相关的库和解释器时的策略变化有关。从CMake 3.12开始,
FindPythonInterp
和FindPythonLibs
模块已被弃用,并在后续版本中完全移除,取而代之的是FindPython
、FindPython3
等新的模块。这些变化影响了ROS 2的某些部分,特别是与Python接口生成相关的部分。在您的情况下,
rosidl_generator_py
依赖于这些已被弃用的模块,导致编译时产生警告,并且可能间接影响编译过程(尽管您提到通过注释掉某些行可以绕过这个问题)。解决方案
-
更新ROS 2环境:
确保您的ROS 2环境是最新的,特别是如果您正在使用预编译的Windows安装包,可能包含了一些尚未更新以支持最新CMake版本的依赖项。 -
手动设置CMake策略:
在您的CMakeLists.txt
文件中,您可以显式地设置CMake策略以避免这些警告。在文件的顶部添加以下行:cmake_policy(SET CMP0148 NEW)
这告诉CMake使用新的Python查找策略。
-
检查Python环境:
确保您的系统Python环境被CMake正确识别。您可以在命令行中运行cmake --help-policy CMP0148
来获取更多关于如何设置Python查找策略的信息。 -
清理并重新构建:
在做了以上更改后,清理您的构建目录(删除build
或install
目录,取决于您的构建设置),然后重新运行colcon build
。 -
检查ROS 2社区和文档:
查看ROS 2的官方文档或社区论坛,看看是否有其他人遇到并解决了类似的问题。 -
考虑使用Docker或虚拟机:
如果您在Windows上遇到持续的问题,考虑使用Docker或虚拟机来运行ROS 2,这通常可以提供更干净、更一致的环境。
注释掉
find_package(rosidl_default_generators REQUIRED)
的影响注释掉这行代码可能会影响ROS 2接口生成器的可用性,这些生成器对于生成消息和服务的C++、Python等语言绑定是必需的。如果您的包没有使用这些生成器(例如,只包含纯C++代码),那么可能暂时不会看到直接影响。然而,如果其他包依赖于由
rosidl_default_generators
生成的接口,那么这可能会在以后导致问题。因此,建议尝试解决上述CMake警告,而不是简单地注释掉必要的
find_package
调用。
以上为生成回答,仅供参考~
-