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\""
		}
	]
}
相关推荐
明月清风徐徐19 小时前
Miniconda + VSCode 的Python环境搭建
ide·vscode·python
王亭_6661 天前
VSCode集成deepseek使用介绍(Visual Studio Code)
ide·vscode·编辑器·deepseek·openrouter
爱健身的小范1 天前
记录一下VScode可以使用nvcc编译,但VS不行的解决方案
ide·vscode·编辑器
翻滚吧键盘1 天前
vscode复制到下一行
ide·vscode·编辑器
柯腾啊1 天前
VSCode 中使用 Snippets 设置常用代码块
开发语言·前端·javascript·ide·vscode·编辑器·代码片段
perseverance521 天前
VSCode自定义快捷键和添加自定义快捷键按键到状态栏
vscode·vscode快捷键配置
今天吃了嘛o2 天前
vscode将文件中行尾默认CRLF改为LF
vscode·编辑器
代码搬运媛2 天前
VSCode AI提效工具,通义灵码前端开发体验
ide·vscode·编辑器
鸡鸭扣2 天前
Docker:3、在VSCode上安装并运行python程序或JavaScript程序
运维·vscode·python·docker·容器·js
m0_748255412 天前
vscode配置django环境并创建django项目(全图文操作)
vscode·django·sqlite