ros项目建立,VScode头文件、变量跳转问题解决

ROS项目建立

1、新建工作空间文件夹,其中包含src文件夹

mkdir -p ros_new/src

2、在src中创建功能包

catkin_create_pkg ros_pkg roscpp rospy std_msgs

3、在ros_pkg目录下src中可以创建cpp源代码

touch helloword.cpp

#include "ros/ros.h"
int main(int argc, char *argv[])
{
  //执行 ros 节点初始化
  ros::init(argc,argv,"hello");
  //创建 ros 节点句柄(非必须)
  ros::NodeHandle n;
  //控制台输出 hello world
  ROS_INFO("hello world!");
  return 0;
}

4、修改ros_pkg目录下CMakeLists.txt

Build下主要添加,节点名称hello,链接到对应的cpp文件

add_executable(hello src/helloworld.cpp)
// add_dependencies(talker beginner_tutorials_generate_messages_cpp)
target_link_libraries(hello ${catkin_LIBRARIES}) 
## 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
# add_library(${PROJECT_NAME}
#   src/${PROJECT_NAME}/ros_pkg.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
# 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
# add_executable(${PROJECT_NAME}_node src/ros_pkg_node.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"
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")

## Add cmake target dependencies of the executable
## same as for the library above
# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

## Specify libraries to link a library or executable target against
# target_link_libraries(${PROJECT_NAME}_node
#   ${catkin_LIBRARIES}
# )

5、在工作空间目录下catkin_make

[100%] Built target hello

6、环境变量配置

.bashrc中添加/home/chen/ros_new/devel/setup.bash

可以将在ros中找到节点

7、source一下

source .bashrc

8、运行程序

roscore
rosrun ros_pkg hello

头文件查找/代码补全/变量跳转相关问题

1、如何生成c_cpp_properities.json,有这个基本上都能包含项目路径了:

ctrl+shift+P打开Command Palette,运行C/Cpp: Edit configurations...生成c_cpp_properties.json

2、生成compile_commands.json

在终端中运行catkin build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON,在build文件夹下生成compile_commands.json

catkin_make和catkin build所起的作用差不多,但是后者有比较复杂的功能

3、在c_cpp_properties.json中添加compile_commands.json所在目录

"compileCommands": "${workspaceFolder}/build/compile_commands.json"

c_cpp_properties.json
{
  "configurations": [
    {
      "browse": {
        "databaseFilename": "${workspaceFolder}/.vscode/browse.vc.db",
        "limitSymbolsToIncludedHeaders": false
      },
      # 头文件路径
      "includePath": [
        "/opt/ros/melodic/include/**",
        "/usr/include/**"
      ],
      "name": "ROS",
      "intelliSenseMode": "gcc-x64",
      "compilerPath": "/usr/bin/gcc",
      "cStandard": "gnu11",
      "cppStandard": "c++14"      
      "compileCommands": "${workspaceFolder}/build/compile_commands.json"
    }
  ],
  "version": 4
}

4、如果还是无法跳转/自动补全,设置中搜索intelli,看看是不是被禁用了

image-20241105160455408

参考文章:【ROS】VSCODE + ROS 配置方法(保姆级教程,总结了多篇)
解决VS Code下找不到ROS头文件和无法自动补全的问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值