Visual Studio Code 2020安裝教程、CPP環境配置

1)下載,直接點下一步安裝即可

官網下載地址:https://code.visualstudio.com/

 

2)安裝cpptools工具

 

3)下載MinGW

下載地址:https://sourceforge.net/projects/mingw-w64/files/

下載的文件:進入網站後不要點擊 "Download Lasted Version",往下滑,找到最新版的 "x86_64-posix-seh"。

安裝MinGW:下載後是一個7z的壓縮包,解壓後移動到你想安裝的位置即可。我的安裝位置是:C:\64-posix-seh\mingw64

 

4)配置環境變量

我的電腦(右鍵屬性)-> 高級系統設置 -> 環境變量 ->點擊 Path 後選 編輯 -> 新建變量(變量值爲bin文件的路徑)

配置對象:WinGW,所以把你剛剛安裝WinGW的路徑拷貝一下

配置環境變量:在此以win10爲例,到達第6步之後,前面打開的窗口都要按下確定,否則會失敗。


 

注:驗證一下環境變量是否配置成功

按下 win + R,輸入cmd,回車鍵之後輸入g++,再回車,如果提示以下信息[1],則環境變量配置成功。

如果提示以下信息[2],則環境變量配置失敗。

 

5)使用簡單的.cpp文件配置C++環境

  • 在電腦桌面新建空文件夾code
  • 打開VScode --> 打開文件夾 --> 選擇剛剛創建的文件夾code
  • 新建test.cpp文件(以最簡單的 HelloWorld.cpp 爲例)
#include <stdio.h>
#include <windows.h>
int main()
{
    printf("Hello World\n");
    system("pause");
    return 0;
}
  • 進入調試界面添加配置環境,選擇 C++(GDB/LLDB),再選擇 g++.exe,之後會自動生成 launch.json 配置文件

  • 編輯 launch.json 配置文件
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,  //修改此項,讓其彈出終端
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\64-posix-seh\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "爲 gdb 啓用整齊打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "task g++" //修改此項 原來是: g++.exe build active file
        }
    ]
}
  • 返回.cpp文件,按F5進行調試,會彈出找不到任務"task g++",選擇 "配置任務",然後選擇g++.exe 會自動生成 tasks.json 文件

  • 編輯 tasks.json 文件
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "task g++",  //修改此項 原來是: g++.exe build active file
            "command": "C:\\64-posix-seh\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\64-posix-seh\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]
}

注:

launch.json 文件中 "preLaunchTask" 的值 必須與 tasks.json 文件中 "label"的值一致。

值的設置看個人喜好,保持默認也是OK的。

 

6)運行

返回 HelloWorld.cpp 文件,按F5調試,發現完全OK了!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章