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\""
		}
	]
}
相关推荐
承接电子控制相关项目3 小时前
安装VSCODE发现 右击选项中无VSCODE 打开选项,处理方法汇总
ide·vscode·编辑器
Better Bench5 小时前
Ubuntu aarch64\arm64系统安装vscode
linux·vscode·ubuntu
x10n95 小时前
OceanBase 参数对比工具 附源码
数据库·vscode·oceanbase·腾讯云ai代码助手
我是菜鸟0713号6 小时前
VSCode配置Qt6开发教程
ide·vscode·编辑器
Jovin Giogic8 小时前
简明教程:记录 Ubuntu系统命令行安装TexLive,配置vscode
linux·vscode·ubuntu·latex·texlive
lanhuazui1010 小时前
VScode中函数和变量跳转定义配置文件设置
vscode·编辑器
oioihoii13 小时前
VSCode CMake Tools 功能解析、流程与最佳实践介绍
ide·vscode·xhtml
fpl111613 小时前
npm :无法加载文件 D:\...\nodejs\npm.ps1,因为在此系统上禁止运行脚本
前端·vscode·npm·node.js·命令模式
啊阿狸不会拉杆13 小时前
告别API碎片化!用AI Ping一键白嫖MiniMax-M2、GLM-4.6与Kimi-K2——清程极智打造的AI路由神器实战指南
人工智能·vscode·ai·语言模型·api
嵌入式学习菌1 天前
vscode配置ESP32-idf存在的问题
ide·vscode·编辑器