一、Vscode对ROS配置
1.1创建ROS工作空间
mkdir -p xxx_ws/src(必须得有 src)
cd xxx_ws
catkin_make
1.2启动 vscode
cd xxx_ws
code .
1.3vscode 中编译 ros
快捷键 ctrl + shift + B 调用编译,选择: catkin_make:build
可以点击配置设置为默认,修改.vscode/tasks.json 文件
{
// 有关 tasks.json 格式的文档,请参见
// https://go.microsoft.com/fwlink/?LinkId=733558
"version": "2.0.0",
"tasks": [
{
"label": "catkin_make:debug", //代表提示的描述性信息
"type": "shell",
//可以选择shell或者process,如果是shell代码是在shell里面
运行一个命令,如果是process代表作为一个进程来运行
"command": "catkin_make",//这个是我们需要运行的命令
"args": [],//如果需要在命令后面加一些后缀,可以写在这里,比如-
DCATKIN_WHITELIST_PACKAGES=“pac1;pac2”
"group": {"kind":"build","isDefault":true},
"presentation": {
"reveal": "always"//可选always或者silence,代表是否输出信息
},
"problemMatcher": "$msCompile"
}
]
}
1.4创建ROS功能包
选定 src 右击 ---> create catkin package
包名 enter roscpp rospy std_msgs
1.5附加
PS1:没有代码提示:修改 .vscode/c_cpp_properties.json 设置 "cppStandard": "c++17"
PS2:int main(int argc, char *argv[])main 函数的参数不可以被 const 修饰
PS3:INFO中文乱码,setlocale(LC_CTYPE, "zh_CN.utf8");setlocale(LC_ALL, "");
二、Vscode配置C++调试
主要修改以下三个文件
2.1c_cpp_properties.json
按下ctrl+shift+p
,选C/C++:Edit Configuration UI
,生成c_cpp_properties.json,一般保持默认的设置不变即可。
{//无需更改保持默认即可
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",//这里是gcc还是g++不影响
"cStandard": "c17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
2.2task.json
按下ctrl+shift+p
,选Tasks: Configure Default Build Task
,生成task.json,做以下修改:
{
"tasks": [
{
"type": "shell",//原本是cppbuild,不改成shell的话可以单独执行task,但
//但无法把task作为launch的先导任务,改完这个将在每次调试的时候自动重新编译
"label": "Build",
"command": "/usr/bin/g++",//CPP使用编译器g++
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"//生成的文件,launch中调试的文件需要与这里保持一致
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger.",
"presentation":{
"panel":"new"},//增加presetation属性,设置打开新panel,防止出现终端被占用的报错
}
],
"version": "2.0.0"
}
2.3launch.json
点击debug图标
生成launch.json
文件以后,文件里面有Add Configuration
按钮,点击,选C/C++: (gdb) Launch
,会生成很多默认配置,做如下修改:
{
// 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": "(gdb) Launch",//Add Configuration选择gdb launch生成下面的信息
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",//和task中生成的文件保持一致
"args": [],//这里是调试的时候想传入的参数
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "Build",//改为task中的label,这样可以每次调试之前重新编译
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}