首页 > 编程语言 > M1 Macbook vscode C++ debug调试实现
2022
04-06

M1 Macbook vscode C++ debug调试实现

这里给出自己摸索的最基本的调试方式,需要进阶调试感觉还是需要一定的学习成本的,尝试了几个网上的博客,暂时没遇到直接可以运行的。所以这里记录一下大概方法。

主要是需要在目录文件下配置两个 json 文件(tasks.json,launch.json)

版本说明

VS code 版本是在官网直接下载的 M1 版本的 February 2021 (version 1.54)
官方下载

扩展

主要是要下载 codeLLDB 的下载,直接在 VS code 里面搜索下载就好了(可能需要从网上下载 VSIX,不过 VS code 会有提示)

配置文件

首先需要有一个文件目录 demo:

选中我们需要调试的文件 test.cpp,然后按 F1,打开设置选项,选择 Tasks:Configure Default Build Task,根据需要选择对应的编译器,这里选择 clang++:

 

之后 VS code 会在同级目录下自动生成一个名为 `tasks.json` 的文件,正常这里是如果没有其他需求直接使用默认的即可,如果需要加入 std=c++11 还是 c++17 之类的,要在 `args` 的内容里添加,这个可以额外学习一下 tasks.json 的配置教程,这里就不赘述了。默认生成内容如下:

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: clang++ 生成活动文件",
			"command": "/usr/bin/clang++",
			"args": [
				"-g",
				"${file}",
				"-o",
				"${fileDirname}/${fileBasenameNoExtension}"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "编译器: /usr/bin/clang++"
		}
	]
}

然后 选择左边第三个调试选项,再选择create a launch.json file

然后要选择 LLDB 选项,这个才是我们下载的 codeLLDB 插件,VS code 会自动创建一个 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": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug",
            "program": "${workspaceFolder}/<your program>",
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

这里需要稍作修改,将 “program” 选项修改成与 tasks.json 的文件名一致,然后还需要加一个 preLaunchTask 的选项,将 tasks.json 的 label 名字粘贴过来,修改以后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": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}",
            "args": [],
            "cwd": "${workspaceFolder}",
            "preLaunchTask": "C/C++: clang++ 生成活动文件"
        }
    ]
}

运行调试

上述配置完成以后,编译项目(shift+command+B),在代码中设置断点,然后直接点击 F5,就可以正常断点运行调试了。

到此这篇关于M1 Macbook vscode C++ debug调试的文章就介绍到这了,更多相关M1 vscode C++ 调试内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

编程技巧