用 vscode debug c++ 代码

1. 写在最前面

最近在看 1.3.6 版本的 Redis 的源码,有些地方没办法直接通过「看」的方式搞懂,觉得「用」才是快速上手的不二法门。笔者所用的编辑器是 vscode,所以就有了这篇如何用 vscode 调试 C++ 代码的记录。

注:笔者所用的操作系统环境为 mac

2. 如何配置文件

2.1 编译 c++ 代码——task.json

2.1.1 创建 task.json 文件

选择 Terminal > Configure Default Build Task. 创建 task.json 的文件。

2.2.2 编辑 task.json 文件

将以下内容 copy 到 task.json 的文件中。

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "shell",
			"label": "clang++ build active file",
			"command": "/usr/bin/clang++",
			"args": [
				"-std=c++17",
				"-stdlib=libc++",
				"-g",
				"${file}",
				"-o",
				"${fileDirname}/${fileBasenameNoExtension}"
			  ],
			"options": {
				"cwd": "${workspaceFolder}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			}
		}
	]
}
2.1.3 编译 cpp 文件

打开 helloworld.cpp 的文件,然后 ⇧⌘B 或者 Terminal -> Run Build Task.

注:此处一定要在打开的 helloworld.cpp 的文件下进行编译,否则上面编译命令中指定引用的文件名会错误

在这里插入图片描述

编译 helloworld.cpp 文件后,生产 helloworld 的执行文件以及 helloworld.dSYM 的 debug 文件。

2.2 调试 C++ 代码 —— launch.json

2.2.1 创建 launch.json 文件

选择 Run -> Add Configuration.

2.2.2 编辑 launch.json

将以下内容 copy 到 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": "clang++ - Build and debug active file",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": true,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "lldb",
        "preLaunchTask": "clang++ build active file"
      }
    ]
  }

注:确保 launch.json 中的 preLaunchTask 的值跟 task.json 中 label 中的值相同

3. 调试

配置完上述的配置文件以后,然后直接点击 vscode 的调试按钮就可以愉快的开始调试啦
在这里插入图片描述

4. 碎碎念

你以为你以为的就是你以为的,配置完上面的文件以后,我以为就能开始调试 redis 的代码了,然而我发现它是个多 cpp 文件的,我还得研究下要怎么调试才行,额,我太难了……

5. 参考资料

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值