VSC C++ Debugging Settings


之前使用的是史上最强IDE :VS来编译+调试,压根不考虑这些

然后我发现VSC上很轻量,所以就试试VSC上编译+调试。所以需要GDB来调试。

而且VSC上对文本编译比VS上快速。这才是我使用的主要原因。

下载MinGW GCC/G++

可以到MinGW 官网去找download

MinGW Installation Manager 安装

我选择安装的是MinGW Installation Manager

GDB setups

之前使用 MinGW Installation Manager 安装 gcc/g++ 时自动给安装上了GDB
在这里插入图片描述

你也可以去 GNU 官网下载 GDB 源码,然后编译出对应平台的gdb.exe。

VSC setup C/C++ plugin

先去安装C/C++插件:Ctrl + Shift + X也可以打开
在这里插入图片描述

就这个:
在这里插入图片描述

安装好就OK了。

VSC c++ Building

g++ -g

然后在g++编译是,添加:-g 参数编译一个 gdb.exe -g [file] -o [filename].exe 来给windows 平台的C++源码构建(编译+链接)出一个带调式信息的.exe。

VSC c++ debugging settings

Setting Building Infos in Terminal

VSC的菜单栏:Terminal->Run Build Task…
在这里插入图片描述
如果没有任何Task的配置信息VSC将自动创建一个tasks.json配置。

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "shell",
			"label": "shell: g++.exe build active file", // Ctrl + Shift + B 显示在Build Task List的名称
			"command": "C:\\MinGW\\bin\\g++.exe", // 默认:只要设置好了MinGW的bin环境变量,g++这里会自动识别到
			"args": [
				"-g", // 默认:编译带可调试信息
				"${file}", // 默认:当前文件,所以编译时你需要打开你的main()入口的文件
				"-o", // 输出
				// "${fileDirname}\\${fileBasenameNoExtension}.exe" // 输出的路径,这里是默认的,下面将调整一下
				"${fileDirname}\\out.exe" // 调整为一个固定的out.exe名称
			],
			"options": {
				"cwd": "C:\\MinGW\\bin"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build"
		}
	]
}

这时再 Ctrl + Shift + B,在弹出的Build Task List列表中选择:我们刚刚创建好的:shell: g++.exe build active file 就可以选择需要Build的选项了。
在这里插入图片描述
编译后,在 Terminal 有显示编辑进度与结果(因为我们这个是Build Active File,所以一定要Active你当前要Build的main入口文件在这里插入图片描述
OK,现在就Building好了。

剩下Debugger设置了。

VSC c++ Debugging

VSC下的Run->Add Configuration…
在这里插入图片描述

如果没有配置过,VSC也是会自动创建 luanch.json

选择 gdb 启动
在这里插入图片描述

luanch.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": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            // "program": "输入程序名称,例如 ${workspaceFolder}/a.exe", // 默认内容,调整一下
            "program": "${workspaceFolder}/out.exe",  // 调整为我们的工程对应编译出来的.exe
            "args": [], // 调试参数可以自己填写在这
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}", // 当前工作空间目录
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            // "miDebuggerPath": "/path/to/gdb", // 这时默认的,需要调整为 gdb.exe 路径
            "miDebuggerPath": "C:/MinGW/bin/gdb.exe", // 这时我的 gdb.exe 路径
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

配置好了,这时我们直接F5调试运行即可。

在这里插入图片描述

  • Variables
  • Watch
  • Call Stack
  • BreakPoints
  • Exception BreakPoints
  • Output
  • Debug Console
  • Terminal

应有尽有。

剩下的调试与VS没什么区别,方便的很。

tasks.json - 该文件是build task 构建任务列表的配置

我们稍微修改一下,其他的编译功能:

编译指定.cpp,而不是active file

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "shell",
			"label": "shell: g++.exe build active file", // Ctrl + Shift + B 显示在Build Task List的名称
			"command": "C:\\MinGW\\bin\\g++.exe", // 默认:只要设置好了MinGW的bin环境变量,g++这里会自动识别到
			"args": [
				"-g", // 默认:编译带可调试信息
				"${file}", // 默认:当前文件,所以编译时你需要打开你的main()入口的文件
				"-o", // 输出
				// "${fileDirname}\\${fileBasenameNoExtension}.exe" // 输出的路径,这里是默认的,下面将调整一下
				"${fileDirname}\\out.exe" // 调整为一个固定的out.exe名称
			],
			"options": {
				"cwd": "C:\\MinGW\\bin"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build"
		},
		{
			"type": "shell",
			"label": "shell: g++.exe a.cpp to out.exe", // Ctrl + Shift + B 显示在Build Task List的名称
			"command": "C:\\MinGW\\bin\\g++.exe", // 默认:只要设置好了MinGW的bin环境变量,g++这里会自动识别到
			"args": [
				"-g", // 默认:编译带可调试信息
				// "${file}", // 默认:当前文件,所以编译时你需要打开你的main()入口的文件
				"a.cpp", // 默认:当前文件,所以编译时你需要打开你的main()入口的文件
				"-o", // 输出
				// "${fileDirname}\\${fileBasenameNoExtension}.exe" // 输出的路径,这里是默认的,下面将调整一下
				"${fileDirname}\\out.exe" // 调整为一个固定的out.exe名称
			],
			"options": {
				"cwd": "C:\\MinGW\\bin"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build"
		}
	]
}

可以看到我们添加了第二个task:shell: g++.exe a.cpp to out.exe

在这里插入图片描述

编译没有问题。

dependsOn - 依赖的前置Task

如果我们的某个Task执行前,需要先执行别的Task,那么可以使用dependsOn
如下:

		{
			"type": "shell",
			"label": "rd Debug/bin/*", // 清理Debug/bin目录下的内容
			"command": "rd",
			"args": [
				"/s", 	// 文件、文件夹一同删除 /s	Deletes a directory tree (the specified directory and all its subdirectories, including all files).
				"/q",	// 安静模式的删除 /q	Specifies quiet mode. Does not prompt for confirmation when deleting a directory tree. (Note that /q works only if /s is specified.)
				"Debug/bin/*"
			],
			"options": {
				"cwd": "${workspaceFolder}" // 设置 compiler 的current workspace directory?
			},
			"group": "build"
		},
		{
			"type": "shell",
			"label": "mkdir Debug/bin", // 创建Debug/bin目录
			"command": "mkdir",
			"args": [
				"Debug/bin"
			],
			"options": {
				"cwd": "${workspaceFolder}" // 设置 compiler 的current workspace directory?
			},
			"group": "build"
		},
		{
			"type": "shell",
			"label": "shell: g++.exe a.cpp to Debug/bin/out.exe", // Ctrl + Shift + B 显示在Build Task List的名称
			"command": "C:\\MinGW\\bin\\g++.exe", // 默认:只要设置好了MinGW的bin环境变量,g++这里会自动识别到
			"args": [
				"-g", // 默认:编译带可调试信息
				"a.cpp", // 默认:当前文件,所以编译时你需要打开你的main()入口的文件
				"-o", // 输出
				"${workspaceFolder}/Debug/bin/out.exe" // 调整为一个固定的out.exe名称
			],
			"options": {
				"cwd": "${workspaceFolder}" // 设置 compiler 的current workspace directory?
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build"
			"dependsOn":[ // 执行该task前先执行dependsOn列表内的task
				"rd Debug/bin/*",
				"mkdir Debug/bin"
			]
		},

最后可以看到:

			"dependsOn":[ // 执行该task前先执行dependsOn列表内的task
				"rd Debug/bin/*",
				"mkdir Debug/bin"
			]

实现多cpp文件编译

无效的多cpp文件编译

根据VSC官方:Modifying tasks.json
在这里插入图片描述
结果我在VSC下的g++没有什么乱用
在这里插入图片描述
貌似g++识别不了

有效的多cpp文件编译

稍微尝试修改了一下:options->cwd

			"options": {
				"cwd": "${workspaceFolder}" // jave.lin : 我自己估计是:cwd是:shell, cmd, power shell Current Workspace Directory的简写
			},

还有把:
在这里插入图片描述

		{
			"type": "shell",
			"label": "shell: g++.exe multi cpp/h to out.exe[VALIDATED~~]", // 注意我添加了一个:VALIDATED,有效的
			"command": "C:\\MinGW\\bin\\g++.exe", // 默认:只要设置好了MinGW的bin环境变量,g++这里会自动识别到
			"args": [
				"-g", // 默认:编译带可调试信息
				"*.cpp", // 设置为当前工作空间目录下的所有.cpp,当是g++识别不了
				"-o", // 输出
				"${workspaceFolder}\\out.exe" // 
			],
			"options": {
				"cwd": "${workspaceFolder}" // jave.lin : 我自己估计是:cwd是:shell, cmd, power shell Current Workspace Directory的简写
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build"
		}

运行没毛病
在这里插入图片描述

luanch.json - 执行/运行配置

那么就实现一个:实现,每次执行前,都会自动编译一遍的功能。

luanch.json 添加一个“前置执行任务”(preLaunchTask

前面介绍的 tasks.json 添加一个 building 用的task,如下:

		{
			"type": "shell",
			"label": "shell: BUILDING: g++.exe a.cpp to out.exe", // Ctrl + Shift + B 显示在Build Task List的名称
			"command": "C:\\MinGW\\bin\\g++.exe", // 默认:只要设置好了MinGW的bin环境变量,g++这里会自动识别到
			"args": [
				"-g", // 默认:编译带可调试信息
				"a.cpp", // 默认:当前文件,所以编译时你需要打开你的main()入口的文件
				"-o", // 输出
				"${workspaceFolder}/out.exe" // 调整为一个固定的out.exe名称
			],
			"options": {
				"cwd": "${workspaceFolder}" // 设置 compiler 的current workspace directory?
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build"
		},

然后给 launch.json 添加preLaunchTask
在这里插入图片描述

{
    // 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": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            // "program": "输入程序名称,例如 ${workspaceFolder}/a.exe", // 默认内容,调整一下
            "program": "${workspaceFolder}/out.exe",  // 调整为我们的工程对应编译出来的.exe
            "args": [], // 调试参数可以自己填写在这
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}", // 当前工作空间目录
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            // "miDebuggerPath": "/path/to/gdb", // 这时默认的,需要调整为 gdb.exe 路径
            "miDebuggerPath": "C:/MinGW/bin/gdb.exe", // 这时我的 gdb.exe 路径
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "shell: BUILDING: g++.exe a.cpp to out.exe" // 我们可以添加一个运行前执行Task,指定Task的Label即可
        }
    ]
}

这样就可以实现,每次执行前,都会自动编译一遍。

c_cpp_properties.json - 更多的配置控制

使用 Ctrl + Shift + P,或是菜单栏:View->Command Palette…,命令工具
在这里插入图片描述

在Command Palette弹框中选择:C/C++: Edit Configurations (UI)
在这里插入图片描述

可配置项

其实是对c_cpp_properties.json配置文件的修改,我们选的是UI配置截面。
在这里插入图片描述

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.16299.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/Hostx64/x64/cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}

name - 配置名称

在这里插入图片描述
对应json中的:"name": "Win32",

compilerPath - 编译器

在这里插入图片描述
对应json中的:"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/Hostx64/x64/cl.exe",

点击下来可以查看VSC有识别到的,如果你想用的编译器没有出现,可以自行设置该编译器路径即可。
在这里插入图片描述

上面默认的是MSVC上的64位的cl.exe编译器,是微软的编译器。

compilerArgs - 编译器参数

在这里插入图片描述
我填了个 -O3 级别的代码优化(-g不会处理部分优化,部分只有release才有优化
对应json中:"compilerArgs": [ "-O3" ]

intelliSenseMode - 智能模式

在这里插入图片描述
就是对编辑代码时的辅助工具类似,我是按默认的:msvc-x64的(微软vc的提示风格)。
对应json中的:"intelliSenseMode": "msvc-x64"

includePath - 预处理、编译器处理的包含路径

在这里插入图片描述
Include包含文件的搜索路径,上面是默认值,不需要改动,因为上面有个提示:如果在安装了 Visual Studio 的 Windows 上,或者在 compilerPath 设置中指定了编译器,则无需在此列表中列出系统包含路径。

defines - 宏定义

在这里插入图片描述
我输入了最后一项:__TEST
对应json:

            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE",
                "__TEST"
            ],

cStandard - c 标准

在这里插入图片描述
默认是:c11

对应json:"cStandard": "c11",

cppStandard - c++ 标准

在这里插入图片描述

默认是:c++17

对应json:"cppStandard": "c++17",

还有高级设置就不管了。

对应c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE",
                "__TEST"
            ],
            "windowsSdkVersion": "10.0.16299.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/Hostx64/x64/cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64",
            "compilerArgs": [
                "-O3"
            ]
        }
    ],
    "version": 4
}

上面是在UI设置上填好,对应json中的配置。

当然可以直接在json中编辑,效果都是一样的。

问题

后续弄懂了再回来更新

tasks.json与c_cpp_properties.json的配置冲突

在.cpp源码中有这么一段:

    #ifdef __TEST
    printf("\n ====== has define __TEST Macro in tasks.json's compile args  ======\n");
    #endif

    #ifdef __TEST1
    printf("\n ====== has define __TEST1 Macro in c_cpp_extension.json's compilerArgs  ======\n");
    #endif
    
    #ifdef __TEST2
    printf("\n ====== has define __TEST2 Macro in c_cpp_extension.json's defines  ======\n");
    #endif

然后在tasks.json添加了-D编译参数
在这里插入图片描述

		{
			"type": "shell",
			"label": "shell: BUILDING: g++.exe a.cpp to out.exe", // Ctrl + Shift + B 显示在Build Task List的名称
			"command": "C:\\MinGW\\bin\\g++.exe", // 默认:只要设置好了MinGW的bin环境变量,g++这里会自动识别到
			"args": [
				"-g", // 默认:编译带可调试信息
				"a.cpp", // 默认:当前文件,所以编译时你需要打开你的main()入口的文件
				"-o", // 输出
				"${workspaceFolder}/out.exe", // 调整为一个固定的out.exe名称
				"-D__TEST"
			],
			"options": {
				"cwd": "${workspaceFolder}" // 设置 compiler 的current workspace directory?
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build"
		},

然后是 c_cpp_properties.json 中添加defines, compilerArgs参数
在这里插入图片描述

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE",
                "__TEST2"
            ],
            "windowsSdkVersion": "10.0.16299.0",
            "compilerPath": "C:/MinGW/bin/g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64",
            "compilerArgs": [
                "-D__TEST1"
            ]
        }
    ],
    "version": 4
}

然后运行就很明显 c_cpp_properties.json 中的都不会使用,因为我们的 BUILDING Task 压根没用 c_cpp_properties.json 的配置

在这里插入图片描述
只有 __TEST 的有输出

除了这些编译问题,其他的c_cpp_properties.json 还是有点用的,如:智能提示之类的。

References

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值