使用vscode查bug

具体操作

  1. 修改CMakeList.txt
cpp 复制代码
# set(CMAKE_BUILD_TYPE "Release")//注释Release模式
set(CMAKE_BUILD_TYPE "Debug")//设置为Debug模式
# set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -g")//注释

*这行代码是用来设置 CMake 构建系统中 Release 模式下 C++ 编译器的标志(flags)。
set: 这是 CMake 中用于设置变量的命令,它将一个变量设置为指定的值。
CMAKE_CXX_FLAGS_RELEASE: 这是一个预定义的 CMake 变量,用于存储 Release 模式下 C++ 编译器的标志。
"-O3 -Wall -g": 这是被设置的值,它是一个字符串,包含了一系列的编译器标志。具体解释如下:
-O3: 启用高级优化级别,这通常会提高代码的执行速度。
-Wall: 启用警告信息,让编译器产生更多的警告,帮助开发者发现潜在的问题。
-g: 在生成的可执行文件中包含调试信息,以便在调试时能够获取更多的信息。
因此,这行代码的作用是在 Release 模式下,为 C++ 编译器设置了一些优化和调试相关的标志,以提高代码性能并帮助调试。*
  1. 点击 creat a launch .json file. 在vscdoe右下角会出现一个蓝色按钮"Add Configuration",点击这个蓝色按钮,创建一个launch.json文件,该文件位于工作空间的.vscode隐藏文件夹下。launch.json文件内容默认如下:
cpp 复制代码
{
    // 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": []
}

  1. 以调试ROS程序为例,可以选择ROS: Attach 或 ROS: Launch。其中,ROS: Attach 表示对一个可执行节点(node) debug,ROS: Launch 表示对一个launch 文件 debug。
cpp 复制代码
自动补全选择 ROS: Launch 后的launch.json文件如下,在target: 粘贴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": "ROS: Launch",
        "type": "ros",
        "request": "launch",
        "target": "absolute path to launch file"
    }]
}

然后点击这个三角按钮,程序运行,若出现bug, 可自动捕获异常

cpp 复制代码
自动补全选择 ROS: Attach 后的launch.json文件如下,这是需要rosrun pkg type,而后点击三角按钮 attach/连接 到这个节点, 程序运行,若出现bug,  可自动捕获异常
{
    // 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": "ROS: Attach",
        "type": "ros",
        "request": "attach"
    }]
}
相关推荐
读书札记202214 小时前
visual studio 调试技巧总结
ide·visual studio
袋子(PJ)16 小时前
2026年常见的配置文件格式——为什么程序员永远在折腾缩进和括号?—— 常见配置文件进化史(md文档很详细)
编辑器
hacker70717 小时前
Visual Studio安装教程(C#开发版)
ide·c#·visual studio
千码君201618 小时前
flutter: 分享一下基于trae cn 构建的过程
java·vscode·flutter·kotlin·trae
我才是一卓18 小时前
2026 Python 入门教程,结合 vscode 和 miniforge/miniconda
开发语言·vscode·python
XD74297163619 小时前
科技早报晚报|2026年5月1日:本地优先文档、安卓离线 IDE 与双击即用密码库,今天最值得跟进的 3 个机会
android·ide·科技·科技新闻·开发者工具·本地优先
謓泽19 小时前
【Trae IDE】核心功能详解与使用教程
ide·ai·trea
lzl204019 小时前
VSCode中Codex CLI登录卡在‘Sign in with ChatGPT‘屏幕
ide·vscode·chatgpt·codex
SkyXZ~20 小时前
Mac上使用VScode优雅开发STM32
vscode·stm32·macos
xskukuku1 天前
VSCode中的Codex插件如何调用第三方API
vscode·ai·codex