1. 程式人生 > 其它 >vscode C++開發環境配置教程(教你如何用vscode寫C++)

vscode C++開發環境配置教程(教你如何用vscode寫C++)

vscode C++開發環境配置教程(教你如何用vscode寫C++)
https://www.cnblogs.com/baihualiaoluan/p/10661669.html

 

Update:

  2022.1.19

  更新了視訊教程,歡迎大家觀看

  https://www.bilibili.com/video/BV1mL4y1b7zy/

  2020.6.26

  大家在安裝TDM-GCC別忘了勾選gbd64檔案,感謝這位朋友提出的錯誤

  

  連結已更新

  個人郵箱:[email protected],有問題請私信我,最好附上截圖。

   用了一段時間的cb,Devc++,但一直感覺cb的高亮太差,而Devc++使用體驗差(尤其是程式碼補全功能),換過vs2017,但是由於其太大了,卡頓十分明顯,所以最終選擇了vscode這款輕量級編譯器。由於自己在配置c++開發環境時遇到了不少問題,因此特地寫了這篇博文,希望能夠幫助後來者快速實現c++環境配置。

步驟一:下載vscode

直接進入vscode官網下載即可,要注意選擇與自己系統符合的版本。

官網地址:https://code.visualstudio.com/?wt.mc_id=DX_841432

進入官網,會出現下面這個介面

下載完成後,按照預設安裝即可

開啟vscode

點選左側欄第五個,在搜尋框中輸入"chinese",安裝第一個外掛(漢化),然後輸入"c++",也安裝第一個外掛,然後重啟,繼續進行下一步操作。

 

步驟二:下載編譯環境

在這裡博主用的是TDM-GCC

大家可以百度官網下載或者下面的使用百度網盤連結下載

連結:https://pan.baidu.com/s/1cR1VkOnkitFUtwznm2N6oQ
提取碼:bwez

下載完成後按照預設安裝即可,但要記住TDM-GCC的安裝路徑,在接下來要用到。

步驟三:配置環境變數


在TDM-GCC安裝完成後,我們需要將它的路徑加入到系統的環境變數裡

操作如下:

1.開啟系統的控制面板,然後在紅圈裡搜尋“高階設定”。

2.點選高階系統設定

3.點選環境變數配置

4.在系統變數中點選“path",然後點選編輯,將上面的路徑加入即可。

格式形如C:\TDM-GCC-64\bin。

步驟四:配置vscode裡的環境

 

    在vscode中開啟資料夾->建立新的資料夾->選中,這樣就直接加入到vscode中了;或者直接把一個檔案拖入vscode中。接著,在這個檔案下建立一個.vscode資料夾(注意名字一定是.vscode),這是必須的(一般來說,每個資料夾中都會有這麼一個.vscode配置檔案)。注意:將此資料夾放在常用資料夾頂層,就不需要重複配置了。在.vscode資料夾中,新建兩個(只需兩個)配置檔案,即launch.json、tasks.json。將下列內容複製進去即可:

  tasks.json

{
    "version": "2.0.0",
    "tasks": [{
            "label": "g++",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${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
                }
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

 

launch.json:一定要注意

miDebuggerPath 這一條,要與你TDM-GCC安裝路徑一致,注意在路徑中 '\'要替換為'\\',就像下面一樣。
{
    "version": "0.2.0",
    "configurations": [
    {
        </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">(gdb) Launch</span><span style="color: rgba(128, 0, 0, 1)">"</span>,    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 配置名稱,將會在啟動配置的下拉選單中顯示</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">type</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">cppdbg</span><span style="color: rgba(128, 0, 0, 1)">"</span>,         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 配置型別,這裡只能為cppdbg</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">request</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">launch</span><span style="color: rgba(128, 0, 0, 1)">"</span>,    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 請求配置型別,可以為launch(啟動)或attach(附加)</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">program</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${fileDirname}/${fileBasenameNoExtension}.exe</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 將要進行除錯的程式的路徑</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">args</span><span style="color: rgba(128, 0, 0, 1)">"</span>: [],                <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 程式除錯時傳遞給程式的命令列引數,一般設為空即可</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">stopAtEntry</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(0, 0, 255, 1)">false</span>,     <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 設為true時程式將暫停在程式入口處,一般設定為false</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">cwd</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${workspaceRoot}</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 除錯程式時的工作目錄,一般為${workspaceRoot}即程式碼所在目錄</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">environment</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">: [],
        </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">externalConsole</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(0, 0, 255, 1)">true</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 除錯時是否顯示控制檯視窗,一般設定為true顯示控制檯</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">MIMode</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">gdb</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">miDebuggerPath</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">C:\\TDM-GCC-64\\bin\\gdb64.exe</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> miDebugger的路徑,注意這裡要與MinGw的路徑對應</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">preLaunchTask</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">g++</span><span style="color: rgba(128, 0, 0, 1)">"</span>,    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 除錯會話開始前執行的任務,一般為編譯程式,c++為g++, c為gcc</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">setupCommands</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">: [
            {
                </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">description</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Enable pretty-printing for gdb</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
                </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">text</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">-enable-pretty-printing</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
                </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ignoreFailures</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
            }
        ]
    }
]

}

步驟五:編寫第一個C++程式

在配置完成後

建立一個main.cpp檔案

貼上以下程式碼

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello Vscode" << endl;
    return 0;
}

 

介面應該會變成這樣

 

 因為vscode在執行完程式後會退出,所以可以選擇增加一個斷點或者加一句getchar();

接下來,Ctrl+F5 執行

本文結束,如有疑問,歡迎在評論區留言。

 

Update:

  2022.1.19

  更新了視訊教程,歡迎大家觀看

  https://www.bilibili.com/video/BV1mL4y1b7zy/

  2020.6.26

  大家在安裝TDM-GCC別忘了勾選gbd64檔案,感謝這位朋友提出的錯誤

  

  連結已更新

  個人郵箱:[email protected],有問題請私信我,最好附上截圖。

   用了一段時間的cb,Devc++,但一直感覺cb的高亮太差,而Devc++使用體驗差(尤其是程式碼補全功能),換過vs2017,但是由於其太大了,卡頓十分明顯,所以最終選擇了vscode這款輕量級編譯器。由於自己在配置c++開發環境時遇到了不少問題,因此特地寫了這篇博文,希望能夠幫助後來者快速實現c++環境配置。

步驟一:下載vscode

直接進入vscode官網下載即可,要注意選擇與自己系統符合的版本。

官網地址:https://code.visualstudio.com/?wt.mc_id=DX_841432

進入官網,會出現下面這個介面

下載完成後,按照預設安裝即可

開啟vscode

點選左側欄第五個,在搜尋框中輸入"chinese",安裝第一個外掛(漢化),然後輸入"c++",也安裝第一個外掛,然後重啟,繼續進行下一步操作。

 

步驟二:下載編譯環境

在這裡博主用的是TDM-GCC

大家可以百度官網下載或者下面的使用百度網盤連結下載

連結:https://pan.baidu.com/s/1cR1VkOnkitFUtwznm2N6oQ
提取碼:bwez

下載完成後按照預設安裝即可,但要記住TDM-GCC的安裝路徑,在接下來要用到。

步驟三:配置環境變數


在TDM-GCC安裝完成後,我們需要將它的路徑加入到系統的環境變數裡

操作如下:

1.開啟系統的控制面板,然後在紅圈裡搜尋“高階設定”。

2.點選高階系統設定

3.點選環境變數配置

4.在系統變數中點選“path",然後點選編輯,將上面的路徑加入即可。

格式形如C:\TDM-GCC-64\bin。

步驟四:配置vscode裡的環境

 

    在vscode中開啟資料夾->建立新的資料夾->選中,這樣就直接加入到vscode中了;或者直接把一個檔案拖入vscode中。接著,在這個檔案下建立一個.vscode資料夾(注意名字一定是.vscode),這是必須的(一般來說,每個資料夾中都會有這麼一個.vscode配置檔案)。注意:將此資料夾放在常用資料夾頂層,就不需要重複配置了。在.vscode資料夾中,新建兩個(只需兩個)配置檔案,即launch.json、tasks.json。將下列內容複製進去即可:

  tasks.json

{
    "version": "2.0.0",
    "tasks": [{
            "label": "g++",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${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
                }
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

 

launch.json:一定要注意

miDebuggerPath 這一條,要與你TDM-GCC安裝路徑一致,注意在路徑中 '\'要替換為'\\',就像下面一樣。
{
    "version": "0.2.0",
    "configurations": [
    {
        </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">(gdb) Launch</span><span style="color: rgba(128, 0, 0, 1)">"</span>,    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 配置名稱,將會在啟動配置的下拉選單中顯示</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">type</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">cppdbg</span><span style="color: rgba(128, 0, 0, 1)">"</span>,         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 配置型別,這裡只能為cppdbg</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">request</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">launch</span><span style="color: rgba(128, 0, 0, 1)">"</span>,    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 請求配置型別,可以為launch(啟動)或attach(附加)</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">program</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${fileDirname}/${fileBasenameNoExtension}.exe</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 將要進行除錯的程式的路徑</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">args</span><span style="color: rgba(128, 0, 0, 1)">"</span>: [],                <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 程式除錯時傳遞給程式的命令列引數,一般設為空即可</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">stopAtEntry</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(0, 0, 255, 1)">false</span>,     <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 設為true時程式將暫停在程式入口處,一般設定為false</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">cwd</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${workspaceRoot}</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 除錯程式時的工作目錄,一般為${workspaceRoot}即程式碼所在目錄</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">environment</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">: [],
        </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">externalConsole</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(0, 0, 255, 1)">true</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 除錯時是否顯示控制檯視窗,一般設定為true顯示控制檯</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">MIMode</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">gdb</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
        </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">miDebuggerPath</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">C:\\TDM-GCC-64\\bin\\gdb64.exe</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> miDebugger的路徑,注意這裡要與MinGw的路徑對應</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">preLaunchTask</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">g++</span><span style="color: rgba(128, 0, 0, 1)">"</span>,    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 除錯會話開始前執行的任務,一般為編譯程式,c++為g++, c為gcc</span>
        <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">setupCommands</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">: [
            {
                </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">description</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Enable pretty-printing for gdb</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
                </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">text</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">-enable-pretty-printing</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
                </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ignoreFailures</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
            }
        ]
    }
]

}

步驟五:編寫第一個C++程式

在配置完成後

建立一個main.cpp檔案

貼上以下程式碼

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello Vscode" << endl;
    return 0;
}

 

介面應該會變成這樣

 

 因為vscode在執行完程式後會退出,所以可以選擇增加一個斷點或者加一句getchar();

接下來,Ctrl+F5 執行

本文結束,如有疑問,歡迎在評論區留言。