VSCode中使用C/C++

安装

安装 Visual Studio Code

官网 https://code.visualstudio.com/Download

安装 MinGW

官网 http://www.mingw.org/
安装之后你需要使用 MingGW Installation Manager 安装需要的Packages,比如gdb,如果你没有C/C++的编译器,你也可以同时安装gcc和g++。
因为MinGW不会自己配置PATH,所以你需要手动将MinGW下的bin文件夹添加到PATH中。配置好后可以在命令行中输入gdb,gcc,g++等检验是否安装成功。

安装扩展

在VSCode的应用商店中安装C/C++的扩展。

安装其他

上面三个安装完成后,就可以满足最简单的需求了,如果你有更高阶的需求,可以安装更多的内容,例如一些可选扩展:

  • C/C++ Snippets(Snippets即重用代码块)
  • C/C++ Advanced Lint(vscode-c-cpp-flylint)(Lint即静态检测)
  • Code Runner
  • Rainbow Brackets:彩虹花括号,但注释掉以后不会变灰,算是一点不足
  • One Dark Pro:大概是VS Code安装量最高的主题
  • GBKtoUTF8:把GBK编码的文档转换成UTF8编码的

或者你也可以使用其他的编译器,这些都不在本文的讨论内容之内。

配置json文件

launch.json

打开项目文件夹,找到你要调试的文件,例如test.c,点击左边工具栏的调试,如图所示点击齿轮按钮,
VSCode
会提示你创建launch.json,点开后,修改成下面内容即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"version": "0.2.0",
"configurations": [
{
"name": "C Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"miDebuggerPath":"E:\\Program\\MinGW\\bin\\gdb.exe",//这里填你gdb(MinGW)的地址
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"preLaunchTask": "gcc",
}]
}

同时配置 gcc 和 g++:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
"version": "0.2.0",
"configurations": [
{
"name": "C Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"miDebuggerPath":"E:\\Program\\MinGW\\bin\\gdb.exe",
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"preLaunchTask": "gcc",
},
{
"name": "C++ Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"miDebuggerPath":"E:\\Program\\MinGW\\bin\\gdb.exe",
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"preLaunchTask": "g++",
}
]
}

tasks.json

上一步完成后,点击调试会提示你配置任务运行程序,点击后随便选择一个,就会打开tasks.json文件,用下面替换即可:

{
    "version": "0.1.0",
    "tasks":[
    {
        "taskName": "gcc", 
        "command": "gcc",
        "args": ["-g","${file}","-o","${fileBasenameNoExtension}.exe"],    // 编译命令参数
        "problemMatcher": {
            "owner": "cpp",
            "fileLocation": ["relative", "${workspaceRoot}"],
            "pattern": {
                "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                "file": 1,
                "line": 2,
                "column": 3,
                "severity": 4,
                "message": 5
            }
        }
    },
    {
        "taskName": "g++",
        "command": "g++",
        "args": ["-g","${file}","-o","${fileBasenameNoExtension}.exe"],    // 编译命令参数
        "problemMatcher": {
            "owner": "cpp",
            "fileLocation": ["relative", "${workspaceRoot}"],
            "pattern": {
                "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                "file": 1,
                "line": 2,
                "column": 3,
                "severity": 4,
                "message": 5
            }
        }        
    }
    ]
}

到此,全部完成。