win10用VSCode和mingw运行c++


Mingw(x86_64-8.1.0-release-win32-seh-rt_v6-rev0.7z)和Vscode(VSCodeUserSetup-x64-1.43.2)资源

链接:https://pan.baidu.com/s/1xBntoJSiaqA55yQH1jteeQ
提取码:aed2


一、下载Mingw

  1. https://sourceforge.net/projects/mingw-w64/files/中选择所需的版本,例如“x86_64-win32-seh”。

在这里插入图片描述

  1. 下载下来的是一个压缩包x86_64-8.1.0-release-win32-seh-rt_v6-rev0.7z,解压到你想安装的位置;

  2. \x86_64-8.1.0-release-win32-seh-rt_v6-rev0\mingw64\bin添加到环境变量中(可以在cmd中输入gcc -v来确认环境变量是否添加成功);
    在这里插入图片描述

二、VSCode-Code Runner

1.安装Code Runner插件

在这里插入图片描述

2.配置.vscde环境

其实这个图形化界面就是配置c_cpp_properties.json,你配好后会发现这里出现文件自动创建出来了。

所以以下两种方法选择其一就行了
在这里插入图片描述

(1)直接创建文件c_cpp_properties.json

  1. 先创建配置文件夹.vscode(PS:这个CPP文件夹就是你以后的工作空间,在这个工作空间下就能运行正常,在别的地方就失败。所以选一个固定的地方。)
    在这里插入图片描述
    在这里插入图片描述
  2. 创建c_cpp_properties.json
    在这里插入图片描述
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            // 这里要改!改成你安装mingw的目录下的/bin/g++.exe
            "compilerPath": "D:/mingw64/bin/g++.exe",
            "cStandard": "c11",		// c用c11
            "cppStandard": "c++17",		// c++17
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}
  1. 保存就好了

(2)图形化配置

  1. View→Command Palette 或者 使用Ctrl+Shift+P快捷键打开VS Code Command Palette,搜索选择"C/C++: Edit Configurations (UI)"
    在这里插入图片描述
    在这里插入图片描述

  2. 更改编辑器路径,改成你安装mingw的目录下的/bin/g++.exe
    在这里插入图片描述

  3. 选择IntelliSense模式为gcc-x64
    在这里插入图片描述

  4. 关闭,就会看见自动生成
    在这里插入图片描述

3.运行

创建文件

在工作空间(我的是CPP文件夹)下创建一个hello.cpp,要打上后缀。
PS:这个hello.cpp不是在.vscode文件夹下,而是和.vscode文件夹是并行的,都是直接在CPP文件夹下。
在这里插入图片描述
2. 内容

#include <iostream>
using namespace std;

int main()
{
    cout << "hello!\n";
}

运行

  • 快捷键Ctrl+Alt+n
    在这里插入图片描述

  • 插件按钮
    在这里插入图片描述

三、VSCode-原生

在这里插入图片描述

  • c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "E:/MyHacker/mingw64/bin/g++.exe", // 这里要改!改成你安装mingw的目录下的/bin/g++.exe
            "cStandard": "c11", // c用c11
            "cppStandard": "c++17", // c++17
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}
  • launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Launch (GDB)", // 配置名称,将会在启动配置的下拉菜单中显示
            "type": "cppdbg", // 配置类型,这里只能为cppdbg
            "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
            "targetArchitecture": "x86", // 生成目标架构,一般为x86或x64,可以为x86, arm, arm64, mips, x64, amd64, x86_64
            "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
            "cwd": "${workspaceRoot}", // 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录
            "environment": [],
            "args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
            "preLaunchTask": "g++", // 调试会话开始前执行的任务,链接到tasks.json
            "externalConsole": false, // 调试时是否显示控制台窗口,一般设置为true显示控制台
            "MIMode": "gdb", // VSCode要使用的调试工具
            // 整齐打印
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "linux": { // 下面是Linux平台下,需要配置的参数,这里暂时不用关心
            },
            "windows": { // 下面是Windows平台下,需要配置的参数
                "miDebuggerPath": "E:/MyHacker/mingw64/bin/gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
            }
        }
    ]
}
  • settings.json
{
    // 解决终端中文乱码
    "terminal.integrated.shellArgs.windows": ["-NoExit", "/c", "chcp 65001"],
    "terminal.integrated.fontFamily": "Lucida Console",
    // c++ 配置
    "files.associations": {
        "array": "cpp",
        "*.tcc": "cpp",
        "istream": "cpp",
        "streambuf": "cpp",
        "string_view": "cpp",
        "iostream": "cpp",
        "atomic": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "unordered_map": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "algorithm": "cpp",
        "map": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "optional": "cpp",
        "string": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "fstream": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "limits": "cpp",
        "new": "cpp",
        "ostream": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "typeinfo": "cpp",
        "list": "cpp",
        "unordered_set": "cpp"
    },
    "C_Cpp.errorSquiggles": "Enabled",
}
  • tasks.json
{
    "version": "2.0.0",
    "command": "g++",
    "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}.exe"//一定要写上"{fileDirname}/",不然可以运行但是调试不了
    ], // 编译命令参数
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": [
            "relative",
            "${workspaceRoot}"
        ],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    },
    "group":{
        "kind": "build",
        "isDefault": true
    }
}

Reference

MinGW-w64安装教程——著名C/C++编译器GCC的Windows版本

在windows上用vscode和mingw开发c++

1.Win10+VsCode的C/CPP编译环境搭建

vscode初学者插件

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值