vscode容器调试使用-1.调试使用深入

1.vscode-python代码调试

创建launch.json文件

bash 复制代码
{
    // 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": "Python Debugger: My Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "cwd":"${workspaceFolder}/Path_to_FlashOcc/",
            "console": "integratedTerminal"
            //"args": [
            //   "${command:pickArgs}"
            //]
        }
    ]
}

1.1 vscode中的${workspaceFolder}等变量

${workspaceFolder} :表示当前workspace文件夹路径,.vscode的绝对路经

${workspaceRootFolderName}:表示workspace的文件夹名,也即Test

${file}:文件自身的绝对路径,也即/home/Coding/Test/.vscode/tasks.json

${relativeFile}:文件在workspace中的路径,也即.vscode/tasks.json

${fileBasenameNoExtension}:当前文件的文件名,不带后缀,也即task

${fileBasename}:当前文件的文件名,tasks.json

${fileDirname}:文件所在的文件夹路径,也即/home/Coding/Test/.vscode

${fileExtname}:当前文件的后缀,也即.json

${lineNumber}:当前文件光标所在的行号

${env:PATH}:系统中的环境变量

1.2 vscode配置文件内容分析

1.2.1 launch.json

launch.json中存放运行或者调试可执行文件时的配置:

bash 复制代码
{
    // 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": "g++ - Build and debug active file",  // 名字随便取
            "type": "cppdbg",
            "request": "launch",
            // program 指定调试那个可执行文件,需要绝对路径
            // ${fileDirname} 当前打开的文件所在的绝对路径,不包括文件名
            // ${fileBasenameNoExtension} 当前打开的文件的文件名,不包括路径和后缀名
            "program": "${fileDirname}/${fileBasenameNoExtension}.out", 
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}", //  cwd 字段来指定应用程序的当前工作目录,访问资源的时候从cwd指定的目录下访问。
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ build active file",  // 在执行lanuch.json之前要做的任务
            "miDebuggerPath": "/bin/gdb"        // 指定调试工具
        }
    ]
}

其他参数解释:

bash 复制代码
${fileBasename}  当前打开的文件名+后缀名,不包括路径
${fileExtname} 当前打开的文件的后缀名
${cwd} the task runner's current working directory on startup
${workspaceFolder} .vscode所在目录的绝对路径

【注】文件名launch.json的前后不能有空格。如果你发现launch.json中明明正确的地方竟然都有红色的波浪线,很可能就是你的文件名launch.json有问题。

1.2.2 tasks.json

tasks.json中存放生成可执行文件的命令:

bash 复制代码
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",  // 任务名,在lanuch.json使用此任务名,从而执行此任务
            "command": "/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                // "${file}",
                "${fileDirname}/*.cpp",   // 编译当前打开的文件所在目录下的所有.cpp文件
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out"  // 目标文件名
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

1.2.3 c_cpp_properties.json

本小节使用cmake生成compile_commands.json,然后在c_cpp_properties.json中配置c_cpp_properties.json文件实现代码的跳转。具体见vscode使用compile_commands.json配置includePath环境.

这里的代码跳转指的是:光标移动到相应函数,然后按"ctrl+点击"就可以进行跳转。如果需要在debug的时候实现代码跳转,直接在launch.json中指定cmake生成的可执行文件就可以。

vscode中配置C/C++调试环境(launch.json和tasks.json)

windows下json文件:

launch.json

bash 复制代码
{
    // 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": "g++.exe - build active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\softwares\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build"
        }
    ]
}

tasks.json

bash 复制代码
{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "build",
			"command": "D:\\softwares\\mingw64\\bin\\g++.exe",
			"args": [
				"-g",
				"${file}",
				"-o",
				"${fileDirname}\\${fileBasenameNoExtension}.exe"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "compiler: \"D:\\softwares\\mingw64\\bin\\g++.exe\""
		}
	]
}
相关推荐
天外飞雨道沧桑11 小时前
前端开发 Cursor MCP 提效工具配置
前端·vscode·ai编程·开发工具·cursor
倦王12 小时前
vscode 中如何去选择不同的远程环境去debug
ide·vscode·编辑器
zhaqonianzhu1 天前
【保姆级】无外网 Linux 服务器用 VSCode 通义灵码:SSH 代理配置全流程
linux·服务器·vscode
Java开发追求者2 天前
vscode导入springboot项目
java·ide·spring boot·vscode
葡萄城技术团队2 天前
Web 无障碍工程化实战:VSCode 扩展 + AI 自动修复全解析
前端·vscode·产品运营
指尖的爷2 天前
VsCode远程开发SSH连接失败
ide·vscode·ssh
不染尘.2 天前
C++中的易混点2
开发语言·数据结构·c++·vscode·算法
shayudiandian2 天前
用VSCode打造AI开发环境
人工智能·vscode
q***71852 天前
【golang学习之旅】使用VScode安装配置Go开发环境
vscode·学习·golang
龙泉寺天下行走2 天前
Vscode 配置C++ Mingw调试、编译环境-无需修改系统PATH变量的VS Code配置方法
c++·ide·vscode