VScode配置最好用的c++环境

本文详细介绍了如何在VSCode中配置C++开发环境,包括launch.json、settings.json和task.json的设置,以及如何解决调试过程中的断点问题。

配置文件的代码和建立方法

  • 话不多说,直接代码:
    • launch.json:
    {
        "version": "0.2.0",
        "configurations": [{
            "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
            "type": "cppdbg", // 配置类型,cppdbg对应cpptools提供的调试功能;可以认为此处只能是cppdbg
            "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
            "args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
            "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,相当于在main上打断点
            "cwd": "${fileDirname}", // 调试程序时的工作目录,此为工作区文件夹;改成${fileDirname}可变为文件所在目录
            "environment": [], // 环境变量
            "externalConsole": true, // 为true时使用单独的cmd窗口,与其它IDE一致;18年10月后设为false可调用VSC内置终端
            "internalConsoleOptions": "neverOpen", // 如果不设为neverOpen,调试时会跳到“调试控制台”选项卡,你应该不需要对gdb手动输命令吧?
            "MIMode": "gdb", // 指定连接的调试器,可以为gdb或lldb。但我没试过lldb
            "miDebuggerPath": "gdb.exe", // 调试器路径,Windows下后缀不能省略,Linux下则不要
            "setupCommands": [
                { // 模板自带,好像可以更好地显示STL容器的内容,具体作用自行Google
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "Compile" // 调试会话开始前执行的任务,一般为编译程序。与tasks.json的label相对应
        }]
    }
    
    • settings.json
    {
        "C_Cpp.errorSquiggles": "Disabled",
        "files.associations": {
            "ostream": "cpp",
            "iostream": "cpp",
            "array": "cpp",
            "atomic": "cpp",
            "*.tcc": "cpp",
            "bitset": "cpp",
            "cctype": "cpp",
            "cfenv": "cpp",
            "chrono": "cpp",
            "cinttypes": "cpp",
            "clocale": "cpp",
            "cmath": "cpp",
            "codecvt": "cpp",
            "complex": "cpp",
            "condition_variable": "cpp",
            "csetjmp": "cpp",
            "csignal": "cpp",
            "cstdarg": "cpp",
            "cstddef": "cpp",
            "cstdint": "cpp",
            "cstdio": "cpp",
            "cstdlib": "cpp",
            "cstring": "cpp",
            "ctime": "cpp",
            "cuchar": "cpp",
            "cwchar": "cpp",
            "cwctype": "cpp",
            "deque": "cpp",
            "forward_list": "cpp",
            "list": "cpp",
            "unordered_map": "cpp",
            "unordered_set": "cpp",
            "vector": "cpp",
            "exception": "cpp",
            "fstream": "cpp",
            "functional": "cpp",
            "future": "cpp",
            "initializer_list": "cpp",
            "iomanip": "cpp",
            "iosfwd": "cpp",
            "istream": "cpp",
            "limits": "cpp",
            "memory": "cpp",
            "mutex": "cpp",
            "new": "cpp",
            "numeric": "cpp",
            "optional": "cpp",
            "ratio": "cpp",
            "scoped_allocator": "cpp",
            "shared_mutex": "cpp",
            "sstream": "cpp",
            "stdexcept": "cpp",
            "streambuf": "cpp",
            "string_view": "cpp",
            "system_error": "cpp",
            "thread": "cpp",
            "type_traits": "cpp",
            "tuple": "cpp",
            "typeindex": "cpp",
            "typeinfo": "cpp",
            "utility": "cpp",
            "valarray": "cpp",
            "random": "cpp",
            "algorithm": "cpp",
            "iterator": "cpp",
            "map": "cpp",
            "memory_resource": "cpp",
            "regex": "cpp",
            "set": "cpp",
            "string": "cpp"
        }
    }
    
    • task.json
    // https://code.visualstudio.com/docs/editor/tasks
    {
        "version": "2.0.0",
        "tasks": [{
            "label": "Compile", // 任务名称,与launch.json的preLaunchTask相对应
            "command": "g++",   // 要使用的编译器,C++用g++
            
            "args": [
                "${file}",
                "-o",    // 指定输出文件名,不加该参数则默认输出a.exe,Linux下默认a.out
                "${fileDirname}/${fileBasenameNoExtension}.exe",
                "-g",    // 生成和调试有关的信息
                "-Wall", // 开启额外警告
                "-static-libgcc",     // 静态链接libgcc,一般都会加上
                "-fexec-charset=GBK", // 生成的程序使用GBK编码,不加这一条会导致Win下输出中文乱码
                "-std=c++14", // C++最新标准为c++17,或根据自己的需要进行修改
            ], // 编译的命令,其实相当于VSC帮你在终端中输了这些东西
            "type": "process", // process是vsc把预定义变量和转义解析后直接全部传给command;shell相当于先打开shell再输入命令,所以args还会经过shell再解析一遍
            "group": {
                "kind": "build",
                "isDefault": true // 不为true时ctrl shift B就要手动选择了
            },
            "presentation": {
                "echo": true,
                "reveal": "always", // 执行任务时是否跳转到终端面板,可以为always,silent,never。具体参见VSC的文档
                "focus": false,     // 设为true后可以使执行task时焦点聚集在终端,但对编译C/C++来说,设为true没有意义
                "panel": "shared"   // 不同的文件的编译信息共享一个终端面板
            },
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": ["relative", "\\"],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            } // 此选项可以捕捉编译时终端里的报错信息;但因为有Lint,再开这个可能有双重报错
        }]
        
    }
    
    • 最后是cpp.json,这个可以是写代码更方便。
    {
    	// Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and 
    	// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
    	// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
    	// same ids are connected.
    	// Example:
    	// "Print to console": {
    	// 	"prefix": "log",
    	// 	"body": [
    	// 		"console.log('$1');",
    	// 		"$2"
    	// 	],
    	// 	"description": "Log output to console"
    	// }
    	"ch" :{
    		"prefix": "ch",
    		"body": [
    			"#include <bits/stdc++.h>",
    			"using namespace std;",
    			"typedef long long ll;",
    			"struct READ {",
    			"    inline char read() {",
    			"    #ifdef _WIN32",
    			"        return getchar();",
    			"    #endif",
    			"        static const int IN_LEN = 1 << 18 | 1;",
    			"        static char buf[IN_LEN], *s, *t;",
    			"        return (s == t) && (t = (s = buf) + fread(buf, 1, IN_LEN, stdin)), s == t ? -1 : *s++;",
    			"    }",
    			"    template <typename _Tp> inline READ & operator >> (_Tp&x) {",
    			"        static char c11, boo;",
    			"        for(c11 = read(),boo = 0; !isdigit(c11); c11 = read()) {",
    			"            if(c11 == -1) return *this;",
    			"            boo |= c11 == '-';",
    			"        }",
    			"        for(x = 0; isdigit(c11); c11 = read()) x = x * 10 + (c11 ^ '0');",
    			"        boo && (x = -x);",
    			"        return *this;",
    			"    }",
    			"} in;",
    			"",
    			"const int N = 2e5 + 50;",
    			"int main() {",
    			"",
    			"    return 0;",
    			"}",
    			""
    		]
    	}
    }
    
  • 建立一个随便的名字的文件夹作为你的c++专用文件夹,即工作目录。然后在这个文件夹下建立.vscode文件夹,把上面的文件放进.vscode夹里。然后在工作目录文件夹下建立任意名字文件夹,或源代码,就能跑了。就像这样建立就行,.vscode和你的源代码文件夹并列放在工作目录下:

在这里插入图片描述

调试时的断点问题

  • 1.调试时第一步没有定位到断点。
    我把断点设在了最后(67行),然而调试时黄箭头居然出现在了中间(49)行。我那天晚上急的头上长满了大包,找了很久错误。
    • 结果令我无奈地笑了:我在工作目录中有一个和当前同名的源代码,断点设在了49行,然后调试时VScode就选了同名代码最靠前的断点停下来开始单步调试。解决方法:记住每个程序调完后把断点清空,省的影响后面的调试。。。
  • 2.黄箭头不出现。
    这个就是代码写崩了。比如代码在断点前出现了死循环,或者数组越界或是其他导致程序没法跑到断点处的情况。所以就该关掉调试检查代码就完了。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值