VSCode与Ros

一.#include “ros.h”

1.我们首先安装ros,和vscode后,在我们建好的workspace中打开终端输入code . ,打开vscode。
2.打开vscode后,在左侧的extention(左侧最后一个图标)。搜索C++和ROS添加。
在这里插入图片描述

3.VS端, Ctrl+Shift+P,打开命令行,输入ROS,会出现ROS Create Catkin package(这个就是catkin_create_pkg ) 确定后,第一步输入pkg的名字,第二步输入依赖项,最后功能包创建成功。

4.pkg创建成功后,我们在pkg的src中创建一个test.cpp文件。

//test cpp
#include"ros/ros.h"
#include"ros/advertise_options.h"

int main(int argv, char** argc)
{
    ros::init(argv,argc,"test");
    ros::NodeHandle n;
    
    ROS_INFO("HELLO");
    ros::spin();
    
    return 0;
    
}

在这里插入图片描述

我们会发现,头文件没有被加入,ros.h出现下滑警示线。ros.h查找不到,而且ros空间的下的函数api没有ide的提示。这也就vscode并没有加入ros函数库的路径。
这时使用鼠标悬浮功能,点击“红色灯泡”,点击edit c_cpp_properties.json选项,vscode会自动在配置文件夹中新建.vscode/文件夹,同时在里面初始化了c_cpp_properties.json文件。

5.首先搞好cmakelist,添加执行路径和生成路径
注意cmake的添加项的编写顺序, add_executable一定要在target_link前
同时生成项目名不能与功能包重名,cpp同名不冲突,所以功能包最好加上pkg后缀。

cmake_minimum_required(VERSION 3.0.2)
project(test)
set(CMAKE_CXX_FLAGS${CMAKE_CXX_FLAGS}"-std=c++11")

find_package(catkin REQUIRED COMPONENTS
  roscpp
  std_msgs
)

catkin_package(
 INCLUDE_DIRS include
 LIBRARIES test
 CATKIN_DEPENDS roscpp std_msgs
 DEPENDS system_lib
)

include_directories(
  include
  ${catkin_INCLUDE_DIRS}
)
add_executable(test11 src/test.cpp)
target_link_libraries(test11 ${catkin_LIBRARIES})

6.我们在工程目录中寻找.vscode中的c_cpp_properties.json文件
在这里插入图片描述
这时我们需要在ws的一级目录下打开终端,用命令行编译我们写的c++代码,同时输出编译信息文件,输入

catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=Yes

这个命令会输出一个compile_commands.json文件在ROS工作空间的build文件夹下面

然后在c_cpp_properties.json文件添加下面一段话。

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

修改后的c_cpp_properties.json文件如下所示:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "gcc-x64",
            "compileCommands": "${workspaceFolder}/build/compile_commands.json"

        }
    ],
    "version": 4
}

这时我们就可以发现我们能引入头文件和索引ROS函数IDE(ros.h虽然还有红色下划线但是不影响键入)

5.这时我们使用Crlt+Shift+b,生成文件。发现没法生成。
因为vscode没有catkin_make设置

vscode没有内置make功能,需要借助Task功能进行配置

Ctrl+shift+P进入命令模式,键入tasks: Configure Task

此时会在.vscode文件夹下面自动生成task.json文件。
这时我们需要修改launch.json和tasks.json
launch时为可以让vs找到启动文件
launch.json

// Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}//devel/lib/test/test11",//修改路径
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

task.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "catkin_make",//修改
            "command": "catkin_make",//修改
            "args": [
					//这里不用放参数
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": false
            },//修改
            "group": {
                "kind": "build",
                "isDefault": true
            }
        } 
    ]
    
}

最后大功告成!

多个工作空间问题。
为了验证流程,又重新创作了一个工作空间。创建新的工作空间和功能包时一定不要复制粘贴之前的cmakelist,要注意工作空间的projectname

在这里插入图片描述
在这里插入图片描述

否则会报上面的错误。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值