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\""
		}
	]
}
相关推荐
AI视觉网奇11 小时前
vscode 默认环境路径
ide·vscode·编辑器
慢一点会很快13 小时前
【Tools】VScode远程调试linux服务器(打断点可视化界面方式)
linux·服务器·vscode
王光环15 小时前
vscode与keil的乱码不兼容问题
ide·vscode·编辑器
萑澈15 小时前
迁移 Visual Studio Code 设置和扩展到 VSCodium
ide·vscode·编辑器
虚空之月&&轮舞者20 小时前
Python与矢量网络分析仪3671E:自动化测试(Vscode)
网络·vscode·python·射频工程
阿幸软件杂货间1 天前
VSCode1.101.0便携版|中英文|编辑器|安装教程
vscode·编辑器
小声读源码2 天前
【部署】win10的wsl环境下调试dify的api后端服务
vscode·python·docker·uv·dify·remote-ssh·pyenv
爱吃程序猿的喵2 天前
基于VSCode+PlatformIO环境的ESP8266的HX1838红外模块
ide·vscode·编辑器·arduino·esp8266·红外线
阿运河2 天前
如何配置 VScode 断点调试Linux 工程代码
linux·ide·vscode
LXL_242 天前
如何安装不同版本的ESP-IDF,并配置Vscode插件,以及在Vscode中切换版本
ide·vscode·编辑器