【ROS 入门学习 】CmakeList.txt 和Packages.xml释义

文件系统

转载于 https://mp.weixin.qq.com/s/LwNUvFb5j5acsNkMYH46qQ

ROS 的文件系统 是只在硬盘上 ROS源代码的组织形式:
在这里插入图片描述

架构释义:

WorkSpace

workspace 自定义的工作空间

- build

build 编译空间 ,用于存放CMake 和 catkin 的缓存信息 、配置信息, 和其他中间文件

- devel

devel 开发空间 ,用于存放编译后生成的文件 ,包括头文件 ,动态 & 静态链接库,可执行文件

- src

src 源代码

- - package

package 功能包 (ROS的基本单元 ,包含多个节点 ,库与配置文件 ,包含所有字母小写,只能由字母 数字 与下划线组成)

- - - CmakeLists.txt

CmakeLists.txt 配置 编译规则 ,比如源文件,依赖项 目标文件

- - - package.xml

package.xml 包信息,比如 包名 版本 作者 依赖项
(以前版本是 manifest.xml)

- - - scripts

scripts 存储python文件

— include

include 头文件

- - - msg

msg 消息通信格式文件

- - - srv

srv 服务通信格式文件

- - - action

action 动作格式文件

- - - lanuch

lanuch 可一次运行多个节点

- - - config

config 配置信息

- - CMakeLists.txt

CMakeLists.txt – 编译的基本配置

CMakeLists.txt 文件释义 (基本配置 )1

cmake_minimum_required(VERSION 3.0.2)   
#  所需 cmake 版本
project(ros02_hello_vscode)        
  #  包名称,会被 ${PROJECT_NAME} 的方式调用
  ------------------------------------------------------

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages

## `查找catkin宏和库
##  如果组件列表如find_package(catkin所需组件xyz)
##  是用过的,还可以找到其他的catkin包
-----------------------------------------------------------------


#  设置构建所需要的软件包
find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  geometry_msgs
  message_generation
)
---------------------------------------------------------------
## System dependencies are found with CMake's conventions
#  默认添加系统依赖
# find_package(Boost REQUIRED COMPONENTS system)
----------------------------------------------------------------
## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## 如果包具有setup.py,则取消对此的注释。此宏确保安装其中声明的模块和全局脚本
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
#  启动 python 模块支持
# catkin_python_setup()
-----------------------------------------------------------------

CMakeLists.txt 文件释义 (基本配置 )2

################################################
## Declare ROS messages, services and actions ##
##           声明 ROS 消息、服务、动作           ##
################################################
------------------------------------------------------------------------------

## To declare and build messages, services or actions from within this
##  要在此包中声明和生成消息、服务或操作,请执行以下步骤: 
-----------------------------------------------------------------------------
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
##   your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
##   * add a build_depend tag for "message_generation"
##   * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
##   * If MSG_DEP_SET isn't empty the following dependency has been pulled in
##     but can be declared for certainty nonetheless:
##     * add a exec_depend tag for "message_runtime"

##  *MSG_DEP_SET是您在中使用其消息类型的包集
##  您的消息/服务/操作(例如std_msgs、actionlib_msgs等)。
##  *在package.xml文件中:
##  *为“消息生成”添加构建依赖标记
##  *为MSG_DEP_SET中的每个包添加build_depend和exec_depend标记
##  *如果MSG_DEP_SET不为空,则已拉入以下依赖项
##  但可以肯定地宣布:
##  *为“消息运行时”添加exec_dependen标记
-------------------------------------------------------------------
## * In this file (CMakeLists.txt):
##   * add "message_generation" and every package in MSG_DEP_SET to
##     find_package(catkin REQUIRED COMPONENTS ...)
##   * add "message_runtime" and every package in MSG_DEP_SET to
##     catkin_package(CATKIN_DEPENDS ...)
##   * uncomment the add_*_files sections below as needed
##     and list every .msg/.srv/.action file to be processed
##   * uncomment the generate_messages entry below
##   * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)

##  *在此文件(CMakeLists.txt)中:
##  *添加“消息生成”并将MSG_DEP_中的每个包设置为
##  查找包装(catkin所需组件…)
##  *添加“message_runtime”并将MSG_DEP_中的每个包设置为
##  柳絮包装(catkin包装视情况而定)
##  *根据需要取消注释下面的“添加文件”部分
##  并列出要处理的每个.msg/.srv/.action文件
##  *取消注释下面的generate_messages条目
##  *在MSG_DEP_SET中添加每个包以生成_消息(依赖项…)
------------------------------------------------------------------------
# Generate messages in the 'msg' folder
# 在“msg”文件夹中生成消息 
add_message_files(
  FILES
  Topic.msg
)

## Generate services in the 'srv' folder
##  在“srv”文件夹中生成服务
# add_service_files(
#   FILES
#   Service1.srv
#   Service2.srv
# )

## Generate actions in the 'action' folder
## 在“action”文件夹中生成操作
# add_action_files(
#   FILES
#   Action1.action
#   Action2.action
# )

## Generate added messages and services with any dependencies listed here
# 生成消息、服务时的依赖包
generate_messages(
  DEPENDENCIES
  geometry_msgs
  std_msgs
)

################################################
## Declare ROS dynamic reconfigure parameters ##
##          声明 ROS 动态参数配置                ##
################################################

## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
##   * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
##   * add "dynamic_reconfigure" to
##     find_package(catkin REQUIRED COMPONENTS ...)
##   * uncomment the "generate_dynamic_reconfigure_options" section below
##     and list every .cfg file to be processed
##  若要在此包范围内声明和生成动态重新配置参数,请执行以下操作:
##  *在package.xml文件中:
##  *为“动态重新配置”添加构建依赖和执行依赖标记
##  *在此文件(CMakeLists.txt)中:
##  *将“dynamic_reconfigure”添加到
##  查找包装(catkin所需组件…)
##  *取消注释下面的“generate_dynamic_reconfigure_options”部分
##  并列出要处理的每个.cfg文件

## Generate dynamic reconfigure parameters in the 'cfg' folder
##  在“cfg”文件夹中生成动态重新配置参数 
# generate_dynamic_reconfigure_options(
#   cfg/DynReconf1.cfg
#   cfg/DynReconf2.cfg
# )

###################################
## catkin specific configuration ##
##      catkin 特定配置           ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
##  catkin_package宏为包生成cmake配置文件
##  声明要传递给依赖项目的内容
##  INCLUDE_DIRS:如果packages包含头文件,请取消对此的注释
##  库:在此项目中创建的依赖项目也需要的库
##  CATKIN_DEPENDS:依赖CATKIN_packages的项目也需要
##  依赖项:依赖项目也需要的此项目的系统依赖项 

# 运行时依赖
catkin_package(
  INCLUDE_DIRS include
  LIBRARIES hello_vscode
  CATKIN_DEPENDS roscpp rospy std_msgs geometry_msgs
  DEPENDS system_lib
  message_runtime
)

###########
## Build ##
###########

## Specify additional locations of header files
## Your package locations should be listed before other locations
# 添加头文件路径,当前程序包的头文件路径位于其他文件路径之前
include_directories(
# include
  ${catkin_INCLUDE_DIRS}
)

## Declare a C++ library
# 声明 C++ 库
# add_library(${PROJECT_NAME}
#   src/${PROJECT_NAME}/hello_vscode.cpp
# )

## Add cmake target dependencies of the library
## as an example, code may need to be generated before libraries
## either from message generation or dynamic reconfigure
# 添加库的 cmake 目标依赖
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

## Declare a C++ executable
## With catkin_make all packages are built within a single CMake context
## The recommended prefix ensures that target names across packages don't collide
# 声明 C++ 可执行文件
add_executable(hello_vs_c src/hello_vs_c.cpp)
add_executable(ssy_c src/ssy_c.cpp)

## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
## target back to the shorter version for ease of user use
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
#  重命名c++可执行文件
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")

## Add cmake target dependencies of the executable
## same as for the library above
#  添加可执行文件的 cmake 目标依赖
add_dependencies(ssy_c ${PROJECT_NAME}_generate_messages_cpp)

## Specify libraries to link a library or executable target against
#  指定库、可执行文件的链接库
target_link_libraries(hello_vs_c
  ${catkin_LIBRARIES}
)
target_link_libraries(ssy_c
  ${catkin_LIBRARIES}
)

#############
## Install 安装 ##
#############

# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html

## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
#  设置用于安装的可执行脚本
catkin_install_python(PROGRAMS
  scripts/hello_vscode_p.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

## Mark executables for installation
# 标记要安装的可执行文件
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html
# install(TARGETS ${PROJECT_NAME}_node
#   RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )

## Mark libraries for installation
##  标记要安装的库
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html
# install(TARGETS ${PROJECT_NAME}
#   ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
#   LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
#   RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
# )

## Mark cpp header files for installation
##  标记要安装的cpp头文件
# install(DIRECTORY include/${PROJECT_NAME}/
#   DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
#   FILES_MATCHING PATTERN "*.h"
#   PATTERN ".svn" EXCLUDE
# )

## Mark other files for installation (e.g. launch and bag files, etc.)
##  标记要安装的其他文件(例如,启动和打包文件等) 
# install(FILES
#   # myfile1
#   # myfile2
#   DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
# )

#############
## Testing ##
#############

## Add gtest based cpp test target and link libraries
## 添加基于gtest的cpp测试目标和链接库 
# catkin_add_gtest(${PROJECT_NAME}-test test/test_hello_vscode.cpp)
# if(TARGET ${PROJECT_NAME}-test)
#   target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()

## Add folders to be run by python nosetests
## 添加要由python测试运行的文件夹 
# catkin_add_nosetests(test)


Package.xml文件释义

(功能包(ROS基本单元)包含多个节点、库与配置文件,包名所有字母小写,只能由字母、数字与下划线组成):

<?xml version="1.0"?>
<!-- 格式: 以前是 1,推荐使用格式 2 -->
<package format="2">
  <!-- 包名 -->
  <name>ros02_hello_vscode</name>
  <!-- 版本 -->
  <version>0.0.0</version>
  <!-- 描述信息 -->
  <description>The ros02_hello_vscode package</description>

  <!-- 需要一个维护人员标签,允许多个,每个标签一人 -->
  <!-- 例如:  -->
  <!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
  <!-- 维护者 -->
  <maintainer email="joes@todo.todo">joes</maintainer>


  <!--  需要一个许可证标签,允许多个,每个标签一个许可证  -->
  <!--  常用的许可证字符串 : -->
  <!--    BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
  <!-- 许可证信息,ROS核心组件默认 BSD -->
  <license>TODO</license>


  <!--  Url标记是可选的,但允许多个,每个标记一个  -->
  <!--  可选属性类型可以是:网站、bugtracker或存储库  -->
  <!-- 例如: -->
  <!-- <url type="website">http://wiki.ros.org/hello_vscode</url> -->


  <!--  作者标记是可选的,允许多个,每个标记一个  -->
  <!--  作者不必是维护者,但也可以是维护者  -->
  <!-- 例如: -->
  <!-- <author email="jane.doe@example.com">Jane Doe</author> -->



<!-- *依赖标记用于指定依赖项-->
<!-- 依赖项可以是catkin包或系统依赖项-->
<!-- 示例:-->
<!-- 使用depend作为生成依赖项和执行依赖项包的快捷方式-->
<!--    <depend>roscpp</depend>-->
<!--    请注意,这相当于以下内容:-->
<!--    <build_depend>roscpp</build_depend>-->
<!--    <exec_depend>roscpp</exec_depend>-->
<!-- 对编译时需要的包使用build_depend:-->
<!--    <build_depend>message_generation</build_depend>-->
<!-- 对需要的包使用build_export_depend,以便根据此包进行生成:-->
<!--    <build_export_depend>message_generation</build_export_depend>-->
<!-- 使用buildtool_depend于构建工具包:-->
<!--    <buildtool_depend>catkin</buildtool_depend>-->
<!-- 对于运行时需要的包,请使用exec_depend:-->
<!--    <exec_depend>message_runtime</exec_depend>-->
<!-- 对仅用于测试的软件包使用test_depend:-->
<!--    <test_depend>gtest</test_depend>-->
<!-- 对仅用于生成文档的包使用doc_depend:-->
<!--    <doc_depend>doxygen</doc_depend>--> 

  <!-- 依赖的构建工具,这是必须的 -->
  <buildtool_depend>catkin</buildtool_depend>

  <!-- 指定构建此软件包所需的软件包 -->
  <build_depend>roscpp</build_depend>
  <build_depend>rospy</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_depend>geometry_msgs</build_depend>

  <!-- 指定根据这个包构建库所需要的包 -->
  <build_export_depend>roscpp</build_export_depend>
  <build_export_depend>rospy</build_export_depend>
  <build_export_depend>std_msgs</build_export_depend>
  <build_export_depend>geometry_msgs</build_export_depend>

  <!-- 运行该程序包中的代码所需的程序包 --> 
  <exec_depend>roscpp</exec_depend>
  <exec_depend>rospy</exec_depend>
  <exec_depend>std_msgs</exec_depend>
  <exec_depend>geometry_msgs</exec_depend>
  <exec_depend>message_runtime</exec_depend>


 

<!-- 导出标记包含其他未指定的标记--> 
  <export>
    <!-- 其他工具可以请求在此处放置其他信息-->

  </export>
</package>
<think>好的,我现在需要帮助用户解决在VSCode中配置ROS环境时遇到的找不到ros.h文件的问题,以及正确设置launch文件的问题。首先,我需要回忆一下ROS开发中常见的配置问题解决方案。 用户提到的错误是关于找不到_rclpy_action模块,但当前的问题是关于ros.h文件。ros.h通常是ROS1中的头文件,而用户之前的问题涉及ROS2的rclpy模块。这可能意味着用户可能在混合使用ROS1ROS2的环境,或者项目配置有误。需要确认用户使用的是ROS1还是ROS2,因为两者的配置方式不同。 首先,ros.h属于ROS1的roscpp客户端库,通常位于/opt/ros/[distro]/include目录下。如果用户使用的是ROS2,那么可能需要安装ROS1的相关包或者调整项目配置。但根据用户之前的引用,他们提到的是ROS2的launch文件问题,所以可能存在混淆。 接下来,用户可能在VSCode中配置C++项目时,没有正确设置include路径,导致编译器找不到ros.h。解决方案通常涉及在c_cpp_properties.json中添加ROS的include目录。此外,编译ROS项目通常需要用到catkin_make或colcon,所以需要确保VSCode的任务配置正确。 另外,用户提到设置launch,可能是指调试launch文件时的配置。在ROS1中,launch文件是XML格式,而ROS2使用的是Python脚本。如果用户是在调试ROS1的节点,需要配置launch.json来启动roslaunch,而ROS2可能需要使用ros2 launch命令。但根据之前的错误,用户可能在尝试调试ROS2的Python launch文件,这时需要确保Python解释器路径正确,并且ROS2环境已正确加载。 可能的步骤包括: 1. 确认ROS版本(ROS1还是ROS2)及对应的安装。 2. 检查ROS环境变量是否在VSCode中正确加载,可能需要通过source setup.bash。 3. 配置includePath在VSCode的C/C++扩展中,添加ROS的include目录。 4. 设置tasks.json用于构建项目,使用catkin_make或colcon。 5. 配置launch.json以正确启动节点或launch文件。 6. 确保Python解释器与ROS版本兼容,特别是混合环境中的问题。 需要提醒用户注意区分ROS1ROS2的不同配置,特别是头文件launch文件的差异。此外,环境变量的加载是关键,因为VSCode启动时可能不会自动source ROS的setup文件,导致找不到相关库头文件。 最后,用户可能还需要检查是否安装了必要的ROS包,比如roscpp或rclcpp,以及对应的开发文件是否存在。如果ros.h缺失,可能需要安装ros-<distro>-roscpp包。</think>### 解决方案:VSCode配置ROS环境以找到ros.h并设置launch文件 #### 1. 确认ROS版本与环境配置 - **ROS1与ROS2区别**: `ros.h`属于ROS1的`roscpp`库头文件,而引用中提到的`rclpy`属于ROS2。若项目混合使用两者,需明确版本: - ROS1路径:`/opt/ros/noetic/include/ros` - ROS2路径:`/opt/ros/foxy/include/rclcpp` - **检查安装**: 确保已安装对应版本的ROS包: ```bash # ROS1 sudo apt install ros-<distro>-roscpp # ROS2 sudo apt install ros-<distro>-rclcpp ``` #### 2. 配置VSCode的C/C++头文件路径 1. 创建/修改`.vscode/c_cpp_properties.json`: ```json { "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", "/opt/ros/<distro>/include" # 替换为实际ROS版本路径(如noetic或foxy) ], "defines": [], "compilerPath": "/usr/bin/gcc", "cStandard": "gnu17", "cppStandard": "c++17" } ], "version": 4 } ``` - 若使用ROS2,需添加`/opt/ros/foxy/include/**`。 #### 3. 加载ROS环境变量 VSCode默认不加载`~/.bashrc`中的ROS环境变量,需在终端手动加载: 1. 在VSCode中按`Ctrl+Shift+~`打开终端,执行: ```bash source /opt/ros/<distro>/setup.bash ``` 2. 或通过`tasks.json`自动加载: ```json { "version": "2.0.0", "tasks": [ { "label": "Build ROS Project", "type": "shell", "command": "source /opt/ros/<distro>/setup.bash && colcon build", # ROS2使用colcon,ROS1用catkin_make "group": "build" } ] } ``` #### 4. 配置调试launch文件 针对ROS1节点调试: 1. 创建`.vscode/launch.json`: ```json { "version": "0.2.0", "configurations": [ { "name": "ROS Debug Node", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/devel/lib/<package>/<node>", # ROS1节点路径 "args": [], "environment": [ {"name": "ROS_MASTER_URI", "value": "http://localhost:11311"} ], "setupCommands": [ {"text": "source /opt/ros/<distro>/setup.bash"} ] } ] } ``` #### 5. 处理Python解释器冲突(ROS2) 若同时调试ROS2的Python launch文件: 1. 在VSCode中选择与ROS2兼容的Python解释器: - 按`Ctrl+Shift+P` → `Python: Select Interpreter` → 选择`/usr/bin/python3`或ROS2虚拟环境路径。 2. 在`launch.json`中添加ROS2环境变量: ```json { "configurations": [ { "name": "ROS2 Launch Debug", "type": "python", "request": "launch", "program": "${workspaceFolder}/src/<pkg>/launch/<file>.py", "args": [], "env": { "PYTHONPATH": "/opt/ros/foxy/lib/python3.8/site-packages:${env:PYTHONPATH}" } } ] } ``` ### 关键问题排查 - **未找到ros.h**:检查`c_cpp_properties.json`中路径是否正确,或通过终端验证文件是否存在: ```bash find /opt/ros -name "ros.h" ``` - **环境未加载**:在VSCode终端手动执行`echo $ROS_DISTRO`确认环境变量是否生效。 - **混合ROS版本**:避免同时安装ROS1ROS2,或在VSCode中通过工作区隔离环境[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值