VSCode配置cpp环境

下载 VSCode

链接: VSCode 一直下一步
这里全部勾选

安装 C/CPP 扩展

汉化扩展,可不装
简体中文
在这里插入图片描述
重启VSCode
新建一个文件夹,在文件夹里面新建一个 *.cpp 文件,随便写点代码

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

    for (const string& word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

安装 MinGW 编译器

链接: MinGW 往下滑,下载最新的 x86_64-posix-sjlj,解压即可,路径不要有中文、空格和特殊字符
配置环境变量
在这里插入图片描述
验证是否安装成功
在这里插入图片描述

配置 *.json 文件
  • tasks.json (build instructions)
  • launch.json (debugger settings)
  • c_cpp_properties.json (compiler path and IntelliSense settings)

用 VSCode 打开某个文件夹,该文件夹即成为工作区。工作区还需建立一个 .vscode 文件夹,里面存放上述三个 json 文件

  1. 创建 tasks.json(快捷键 ctrl + shift + p)
    在这里插入图片描述
    在这里插入图片描述
    注:编译器路径视安装位置而定
{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: g++.exe 生成活动文件",
			"command": "D:\\Application\\mingw64\\bin\\g++.exe",
			"args": [
				"-g",
				"${file}",
				"-o",
				"${fileDirname}\\${fileBasenameNoExtension}.exe"
			],
			"options": {
				"cwd": "D:\\Application\\mingw64\\bin"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "编译器: D:\\Application\\mingw64\\bin\\g++.exe"
		}
	]
}
  1. 创建 launch.json(按F5)
    在这里插入图片描述
    在这里插入图片描述
"console": "externalTerminal",
// 改为以下,true 表示从外部显示控制台
"externalConsole": true,
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "D:\\Application\\mingw64\\bin",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\Application\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 生成活动文件"
        }
    ]
}
  1. 创建 c_cpp_properties.json(快捷键 ctrl + shift + p)
    在这里插入图片描述

在这里插入图片描述

完结撒花:理论上经过上述操作,便可编译、调试和运行 cpp 代码啦
在这里插入图片描述
参考教程:
https://code.visualstudio.com/docs/languages/cpp
https://code.visualstudio.com/docs/cpp/config-msvc

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木木+江鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值