跳转至

vscode

单文件调试

Warning

先安装插件 CodeLLDB 替代 vscode 自带的 LLDB 插件,功能更强大,还能在 集成终端中输入输出 ,而不必打开外部终端

tasks.json

用来执行调试前 编译 的配置文件

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "clang++ build active file",
            "type": "shell",
            "command": "clang++",
            "args": [
                "${fileBasename}",
                "-o",
                "${fileBasenameNoExtension}",
                "-g"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

launch.json

用来执行 调试 的启动配置文件

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "lldb",
            "type": "lldb",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "cwd": "${workspaceFolder}",
            "preLaunchTask": "clang++ build active file"
        }
    ]
}

Note

  1. "program":可执行文件位置
  2. "preLaunchTask":执行 launch.json 之前需要执行的任务(一般为编译过程,故应保持与 tasks.json 中的 "label" 项一致)

CMake 调试

安装插件 CMake Tools

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "make",
            "type": "shell",
            "options": {
                "cwd": "${workspaceRoot}/build"
            },
            "command": "make",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

上面的代码等价于

cd build
make

Note

其实在执行上面的 make 之前,还需要执行下面的命令来创建编译项目所需的makefile文件及其他相关文件

mkdir build
cd build
cmake ..
CMake Tools 插件可以帮我们快速完成上述操作,只需按键:command + P ,在输入框中输入
>cmake
选择 CMake: Clean Rebuild 执行即可

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "lldb",
            "type": "lldb",
            "request": "launch",
            "program": "${workspaceFolder}/build/main",
            "args": [],
            "cwd": "${workspaceFolder}",
            "preLaunchTask": "make"
        }
    ]
}

Note

使用 CMake 编译,修改 "preLaunchTask" 项为 makeprogram 项修改为 CMakeLists.txt 中生成文件位置

调试:运行到光标处

调试栏中没有这一项,但可以通过快捷键实现

打开左下角 Settings-Keyboard Shortcuts ,搜索 run to cursor ,设置你喜欢的快捷键即可

快捷键

重命名函数 or 变量

f2

跳转到特定行

control + g

跳转到特定符号块

command + shift + o

跳转到工作区中变量或函数

command + t

代码格式整理

shift + option + F

评论