当前位置:   article > 正文

Ubuntu下Vscode对ROS,C++调试配置_ros 差++代码调试

ros 差++代码调试

一、Vscode对ROS配置

1.1创建ROS工作空间

  1. mkdir -p xxx_ws/src(必须得有 src)
  2. cd xxx_ws
  3. catkin_make

1.2启动 vscode

  1. cd xxx_ws
  2. code .

1.3vscode 中编译 ros

快捷键 ctrl + shift + B 调用编译,选择: catkin_make:build
可以点击配置设置为默认,修改.vscode/tasks.json 文件

  1. {
  2. // 有关 tasks.json 格式的文档,请参见
  3. // https://go.microsoft.com/fwlink/?LinkId=733558
  4. "version": "2.0.0",
  5. "tasks": [
  6. {
  7. "label": "catkin_make:debug", //代表提示的描述性信息
  8. "type": "shell",
  9. //可以选择shell或者process,如果是shell代码是在shell里面
  10. 运行一个命令,如果是process代表作为一个进程来运行
  11. "command": "catkin_make",//这个是我们需要运行的命令
  12. "args": [],//如果需要在命令后面加一些后缀,可以写在这里,比如-
  13. DCATKIN_WHITELIST_PACKAGES=“pac1;pac2”
  14. "group": {"kind":"build","isDefault":true},
  15. "presentation": {
  16. "reveal": "always"//可选always或者silence,代表是否输出信息
  17. },
  18. "problemMatcher": "$msCompile"
  19. }
  20. ]
  21. }

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,一般保持默认的设置不变即可。

  1. {//无需更改保持默认即可
  2. "configurations": [
  3. {
  4. "name": "Linux",
  5. "includePath": [
  6. "${workspaceFolder}/**"
  7. ],
  8. "defines": [],
  9. "compilerPath": "/usr/bin/gcc",//这里是gcc还是g++不影响
  10. "cStandard": "c17",
  11. "cppStandard": "gnu++14",
  12. "intelliSenseMode": "linux-gcc-x64"
  13. }
  14. ],
  15. "version": 4
  16. }

2.2task.json

按下ctrl+shift+p,选Tasks: Configure Default Build Task,生成task.json,做以下修改:

  1. {
  2. "tasks": [
  3. {
  4. "type": "shell",//原本是cppbuild,不改成shell的话可以单独执行task,但
  5. //但无法把task作为launch的先导任务,改完这个将在每次调试的时候自动重新编译
  6. "label": "Build",
  7. "command": "/usr/bin/g++",//CPP使用编译器g++
  8. "args": [
  9. "-fdiagnostics-color=always",
  10. "-g",
  11. "${file}",
  12. "-o",
  13. "${fileDirname}/${fileBasenameNoExtension}"//生成的文件,launch中调试的文件需要与这里保持一致
  14. ],
  15. "options": {
  16. "cwd": "${fileDirname}"
  17. },
  18. "problemMatcher": [
  19. "$gcc"
  20. ],
  21. "group": {
  22. "kind": "build",
  23. "isDefault": true
  24. },
  25. "detail": "Task generated by Debugger.",
  26. "presentation":{
  27. "panel":"new"},//增加presetation属性,设置打开新panel,防止出现终端被占用的报错
  28. }
  29. ],
  30. "version": "2.0.0"
  31. }

2.3launch.json

点击debug图标
在这里插入图片描述
生成launch.json文件以后,文件里面有Add Configuration按钮,点击,选C/C++: (gdb) Launch,会生成很多默认配置,做如下修改:

  1. {
  2. // Use IntelliSense to learn about possible attributes.
  3. // Hover to view descriptions of existing attributes.
  4. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  5. "version": "0.2.0",
  6. "configurations": [
  7. {
  8. "name": "(gdb) Launch",//Add Configuration选择gdb launch生成下面的信息
  9. "type": "cppdbg",
  10. "request": "launch",
  11. "program": "${fileDirname}/${fileBasenameNoExtension}",//和task中生成的文件保持一致
  12. "args": [],//这里是调试的时候想传入的参数
  13. "stopAtEntry": false,
  14. "cwd": "${fileDirname}",
  15. "environment": [],
  16. "externalConsole": false,
  17. "MIMode": "gdb",
  18. "preLaunchTask": "Build",//改为task中的label,这样可以每次调试之前重新编译
  19. "setupCommands": [
  20. {
  21. "description": "Enable pretty-printing for gdb",
  22. "text": "-enable-pretty-printing",
  23. "ignoreFailures": true
  24. },
  25. {
  26. "description": "Set Disassembly Flavor to Intel",
  27. "text": "-gdb-set disassembly-flavor intel",
  28. "ignoreFailures": true
  29. }
  30. ]
  31. }
  32. ]
  33. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/496716
推荐阅读
相关标签
  

闽ICP备14008679号