VS Code c++环境配置

一、下载c++编译器及配置

  1. 下载MinGW-W64 编译器(32和64版本都有)
    MinGW-W64 GCC下载地址:https://sourceforge.net/projects/mingw-w64/files/Toolchains targetting Win32/Personal Builds/mingw-builds/installer/mingw-w64-install.exe
     
  2. 安装MinGW-W64 编译器
    我的电脑是64位的,安装时设置如下:
    (ps:在右键--以管理员身份运行时,发现报错“Cannot download repository.txt”,找到解决方案:在安装时,使用非电信或者联通的手机热点,移动不行,即可解决以上报错,并弹出以下窗口)VS Code c++环境配置_第1张图片
  3. 配置环境变量
    打开你的mingw-w64安装目录,将里面的bin文件夹加入路径即可,如下图所示:
    配置完环境变量需要重启电脑
  4. 在这里插入图片描述

二、下载vs code及配置

  1. 下载vscode:https://code.visualstudio.com/#alt-downloads
  2. 安装vs code相关插件
    安装中文插件:vscode设置中文环境教程http://www.chuancn.cn/post/将VSCode设置成中文语言环境
    c++插件
    在这里插入图片描述
  3. 配置c++的.vscode文件
    (1)在你写代码的地方新建文件夹和里面新建一个文件夹
    (2)然后在文件夹里面创建一个.vscode文件夹
    在这里插入图片描述
    (3)然后在.vscode文件夹里面创建如上图所示的四个文件
    (3.1)launch.json:
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
                "type": "cppdbg", // 配置类型,这里只能为cppdbg
                "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
                "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
                "args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
                "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
                "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录 workspaceRoot已被弃用,现改为workspaceFolder
                "environment": [],
                "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台
                "MIMode": "gdb",
                "miDebuggerPath": "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
                "preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": false
                    }
                ]
            }
        ]
    }
    
    (ps:由于安装路径不同,应对应修改以下部分代码:)
    VS Code c++环境配置_第2张图片
    (3.2)task.json:
    {
        "version": "2.0.0",
        "command": "g++",
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileBasenameNoExtension}.exe"
        ], // 编译命令参数
        "problemMatcher": {
            "owner": "cpp",
            "fileLocation": [
                "relative",
                "${workspaceFolder}"
            ],
            "pattern": {
                "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                "file": 1,
                "line": 2,
                "column": 3,
                "severity": 4,
                "message": 5
            }
        }
    }
    
    (3.3)c_cpp_properties.json:
    {
        "configurations": [
            {
                "name": "posix",
                "includePath": [
                    "${workspaceRoot}",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/include/**",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                    "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
                ],
                "defines": [
                    "_DEBUG",
                    "UNICODE",
                    "__GNUC__=6",
                    "__cdecl=__attribute__((__cdecl__))"
                ],
                "intelliSenseMode": "msvc-x64",
                "browse": {
                    "limitSymbolsToIncludedHeaders": true,
                    "databaseFilename": "",
                    "path": [
                        "${workspaceRoot}",
                        "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/include/**",
                        "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                        "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                        "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                        "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                        "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                        "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
                    ]
                }
            }
        ],
        "version": 4
    }
    
    (ps:由于安装路径不同,代码的以下圈出部分需要对应修改)
    VS Code c++环境配置_第3张图片
    查询以上几个路径的cmd命令如下(当然,也可以直接把路径前面一部分“C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/..”直接替换为自己的安装路径即可,即配置环境变量时的路径):
    gcc -v -E -x c++ -
    并复制以下部分(注意每一行路径都需要用引号括起来):
    VS Code c++环境配置_第4张图片
    (3.4)settings.json:
    {
        "files.associations": {
            "iostream": "cpp"
        },
        "files.exclude": {
            "**/.classpath": true,
            "**/.project": true,
            "**/.settings": true,
            "**/.factorypath": true
        }
    }
    

     

参考文章:https://blog.csdn.net/qq_43041976/article/details/100542557

 

你可能感兴趣的:(笔记,c++,环境配置)