小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
代码报错求助
-
C++创建节点时对代码进行编译报错
cmake_minimum_required (VERSION 3.8)
project (ros2_cpp)
add_executable(rs2_cpp_noode ros2_cpp_node.cpp)
find_package(rclcpp REQUIRED)
target_include_directories(ros2_cpp_node PUBLIC ${rclcpp_INCLUDE_DIRS})
target_link_libraries(ros2_cpp_node ${rclcpp_LTBRARIES})l@l-virtual-machine-22:~/VScode workplace/chapt2$ cmake .
-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found rclcpp: 16.0.10 (/opt/ros/humble/share/rclcpp/cmake)
-- Found Python3: /usr/bin/python3 (found version "3.10.12") found components: Interpreter
-- Found rosidl_generator_c: 3.1.5 (/opt/ros/humble/share/rosidl_generator_c/cmake)
-- Found rosidl_adapter: 3.1.5 (/opt/ros/humble/share/rosidl_adapter/cmake)
-- Found rosidl_generator_cpp: 3.1.5 (/opt/ros/humble/share/rosidl_generator_cpp/cmake)
-- Using all available rosidl_typesupport_c: rosidl_typesupport_fastrtps_c;rosidl_typesupport_introspection_c
-- Using all available rosidl_typesupport_cpp: rosidl_typesupport_fastrtps_cpp;rosidl_typesupport_introspection_cpp
-- Found rmw_implementation_cmake: 6.1.2 (/opt/ros/humble/share/rmw_implementation_cmake/cmake)
-- Found rmw_fastrtps_cpp: 6.2.7 (/opt/ros/humble/share/rmw_fastrtps_cpp/cmake)
-- Found OpenSSL: /usr/lib/x86_64-linux-gnu/libcrypto.so (found version "3.0.2")
-- Found FastRTPS: /opt/ros/humble/include
-- Using RMW implementation 'rmw_fastrtps_cpp' as default
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
CMake Error at CMakeLists.txt:5 (target_include_directories):
Cannot specify include directories for target "ros2_cpp_node" which is not
built by this project.CMake Error at CMakeLists.txt:6 (target_link_libraries):
Cannot specify link libraries for target "ros2_cpp_node" which is not built
by this project.-- Configuring incomplete, errors occurred!
See also "/home/l/VScode workplace/chapt2/CMakeFiles/CMakeOutput.log".
l@l-virtual-machine-22:~/VScode workplace/chapt2$ make
make: *** 没有指明目标并且找不到 makefile。 停止。 -
您遇到的错误主要是因为
target_include_directories
和target_link_libraries
调用时目标名称错误。在您的CMakeLists.txt
文件中,您创建的可执行文件名为rs2_cpp_noode
,但在后续配置中您使用了ros2_cpp_node
。这导致了 CMake 无法找到相应的目标来设置包含目录和链接库。为了解决这个问题,请按照以下步骤修改您的
CMakeLists.txt
文件:-
更正可执行文件的名称:确保您在
add_executable
中使用的名称与后续在target_include_directories
和target_link_libraries
中使用的名称一致。 -
确保在
find_package
调用之后添加可执行文件和库依赖:因为您需要基于找到的包来设置包含目录和链接库。
修改后的
CMakeLists.txt
应该如下所示:cmake_minimum_required (VERSION 3.8) project (ros2_cpp) # 查找rclcpp find_package(rclcpp REQUIRED) # 添加可执行文件 add_executable(ros2_cpp_node ros2_cpp_node.cpp) # 设置包含目录 target_include_directories(ros2_cpp_node PUBLIC ${rclcpp_INCLUDE_DIRS}) # 设置链接库 target_link_libraries(ros2_cpp_node ${rclcpp_LIBRARIES}) # 注意这里修正了变量名,原来是 ${rclcpp_LTBRARIES} # 注意:如果您还有其他依赖,确保在这里也设置好
其他注意事项:
- 确保
ros2_cpp_node.cpp
文件存在并且包含正确的 ROS 2 节点代码。 - 检查
rclcpp_LIBRARIES
是否是您需要的正确变量名(通常这是正确的,但请根据您的 ROS 2 版本和安装情况验证)。 - 确保您的 CMake 版本与 ROS 2 要求的版本兼容。
完成上述修改后,重新运行 CMake 和 make 命令:
cmake . make
如果一切正常,这将编译您的 ROS 2 节点。
以上为生成回答,仅供参考~
-
-