Cmake报错,如何解决?
-
CMakeLists.txt代码如下(跟着书本敲写):
cmake_minimum_required(VERSION 3.8) project(ros2_cpp_node) add_executable(ros2_cpp_node ros2_cpp_node.cpp) find_package(rclcpp REQUIRED) find_package(std_msgs REQUIRED) target_include_directories(ros2_cpp_node PUBLIC ${rclcpp_INCLUDE_DIRS}) target_link_libraries(ros2_cpp_node ${rclcpp_LIBRARIES})
环境检查如下:
jetson@unbutu:~/Documents/chapt2$ echo $CMAKE_PREFIX_PATH /opt/ros/foxy jetson@unbutu:~/Documents/chapt2$ echo $AMENT_PREFIX_PATH /opt/ros/foxy
cmake . 终端输出如下:
jetson@unbutu:~/Documents/chapt2$ cmake . -- Found ament_cmake: 0.9.12 (/opt/ros/foxy/share/ament_cmake/cmake) -- Using PYTHON_EXECUTABLE: /usr/bin/python3 -- Found rclcpp: 2.4.3 (/opt/ros/foxy/share/rclcpp/cmake) -- Using all available rosidl_typesupport_c: rosidl_typesupport_fastrtps_c;rosidl_typesupport_introspection_c -- Found rosidl_adapter: 1.3.1 (/opt/ros/foxy/share/rosidl_adapter/cmake) -- Using all available rosidl_typesupport_cpp: rosidl_typesupport_fastrtps_cpp;rosidl_typesupport_introspection_cpp -- Found rmw_implementation_cmake: 1.0.4 (/opt/ros/foxy/share/rmw_implementation_cmake/cmake) -- Using RMW implementation 'rmw_fastrtps_cpp' as default -- Configuring done CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_generator_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_introspection_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_generator_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_introspection_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_generator_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_introspection_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_generator_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_introspection_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_introspection_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_generator_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_introspection_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_generator_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_introspection_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_generator_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_introspection_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_generator_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_introspection_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_generator_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_introspection_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_generator_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_introspection_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_introspection_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_generator_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_introspection_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_generator_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_introspection_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "std_msgs::std_msgs__rosidl_typesupport_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "rosgraph_msgs::rosgraph_msgs__rosidl_generator_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "rosgraph_msgs::rosgraph_msgs__rosidl_typesupport_introspection_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "rosgraph_msgs::rosgraph_msgs__rosidl_typesupport_c" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "rosgraph_msgs::rosgraph_msgs__rosidl_generator_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "rosgraph_msgs::rosgraph_msgs__rosidl_typesupport_introspection_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? CMake Error at CMakeLists.txt:10 (add_executable): Target "ros2_cpp_node" links to target "rosgraph_msgs::rosgraph_msgs__rosidl_typesupport_cpp" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? -- Generating done CMake Generate step failed. Build files cannot be regenerated correctly.
-
从你提供的CMake输出信息来看,问题在于CMake无法找到
std_msgs
和rosgraph_msgs
中的某些目标(targets),这些目标是由ROS 2的消息生成系统创建的。这通常意味着CMake在配置过程中没有正确地找到或处理这些消息包。解决步骤:
-
确保消息包已安装:
首先,确保std_msgs
和rosgraph_msgs
包已经正确安装在你的ROS 2环境中。你可以通过运行以下命令来安装它们(如果尚未安装):sudo apt install ros-foxy-std-msgs ros-foxy-rosgraph-msgs
-
检查
ament_index
:
ROS 2使用ament_index
来管理和查找安装的包。确保ament_index
是最新的,并且包含了所有必要的包。通常,安装或更新ROS 2包会自动处理这个问题,但有时可能需要手动更新索引:source /opt/ros/foxy/setup.bash ros2 pkg update
-
检查CMakeLists.txt:
你的CMakeLists.txt看起来基本正确,但通常不需要手动设置include目录和链接库,因为find_package
应该处理这些。确保你的CMakeLists.txt使用了正确的ament_package()
和find_package()
调用。例如:cmake_minimum_required(VERSION 3.5) project(ros2_cpp_node) find_package(ament_cmake REQUIRED) find_package(rclcpp REQUIRED) find_package(std_msgs REQUIRED) add_executable(ros2_cpp_node src/ros2_cpp_node.cpp) ament_target_dependencies(ros2_cpp_node rclcpp std_msgs) install(TARGETS ros2_cpp_node DESTINATION lib/${PROJECT_NAME}) ament_package()
注意,这里使用了
ament_target_dependencies
来添加依赖,这是ROS 2推荐的方式。 -
清理并重新构建:
在进行了上述更改后,确保清理旧的构建文件并重新运行CMake:rm -rf build/ mkdir build/ cd build/ cmake .. make
-
检查环境变量:
你已经检查了CMAKE_PREFIX_PATH
和AMENT_PREFIX_PATH
,确保它们包含了ROS 2的安装路径。通常,只需设置CMAKE_PREFIX_PATH
就足够了。 -
查看详细输出:
如果问题仍然存在,尝试在CMake命令中添加-DCMAKE_VERBOSE_MAKEFILE=ON
来获取更详细的输出,这可能会帮助诊断问题。
通过上述步骤,你应该能够解决CMake配置过程中遇到的问题。如果问题仍然存在,可能需要检查ROS 2的安装是否完整或考虑重新安装ROS 2。
以上为生成回答,仅供参考~
-
-
-
@小鱼 有时间可以帮忙看看吗