Visual Studio Code Tutorial (WSL/windows + mingw)

設定 VSCode 環境 (WSL)

NOTE: 安裝教學影片 WSL Tutorial

安裝 WSL & GCC

Install WSL: https://docs.microsoft.com/en-us/windows/wsl/install

啟動 WSL Ubuntu

start ubuntu

WSL Ubuntu 輸入以下指令:

sudo apt update
sudo apt install -y build-essential g++ gdb
g++ -v

Wrong:

g++ wrong

Correct:

g++ correct

安裝 VSCode & Remote - WSL plugin

Install VSCode: https://code.visualstudio.com/docs/setup/windows

Install the Remote - WSL extension for VSCode: https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl

新增專案 (Hello world!)

重新打開 WSL Ubuntu 並輸入以下指令:

$ mkdir projects
$ cd projects
$ code .

Correct:

wsl correct 2

安裝 WSL C++ plugin

Install the C++ extension for VSCode: https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools

Correct:

c++ in wsl

In VSCode: Exploer -> New File

輸入 test.cpp

new file icon

在文字輸入區輸入以下程式碼

#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;
}

input cpp code

編譯 & 執行 Hello world!

In VSCode: Terminal -> New Terminal

create terminal

In VSCode Terminal:

$ g++ test.cpp
$ ./a.out
Hello C++ World from VS Code and the C++ extension! 
$ 

Debugger

可以設置中斷點,方便程式除錯。

launch.json

In VSCode: Exploer -> New Folder

輸入 .vscode

new folder icon

In VSCode: Exploer -> New File

輸入 launch.json

在文字輸入區輸入以下設定檔

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

編譯 & 執行 Debugger

In VSCode Terminal:

$ g++ -g test.cpp
$ 

插入中斷點

在要中斷的程式碼行號左邊點一下

insert breakpoint

In VSCode: Run -> Start Debugging F5

start debugging

run gdb

設定 VSCode 環境 (Windows + mingw)

Credit: CNOCycle/cpp_tutorial by E. Chen

安裝步驟

  1. 下載vscode-all-in-one.zip
  2. 解壓縮縮後,將檔案放到路徑%USERPROFILE%
  3. %USERPROFILE%\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin加入到環境變數Path
  4. 完成

圖解說明

  • %USERPROFILE% 是windows的環境變數,指的是該電腦使用者的目錄。 概念同linux底下的${HOME}。 以本台測試電腦為例,該電腦使用者為WDAGUtilityAccount,則%USERPROFILE%=C:\Users\WDAGUtilityAccount。 正常請況下在檔案總管上的路徑列輸入%USERPROFILE%後會自動跳轉。如下圖示範:

windows_var

  • 將檔案放到路徑%USERPROFILE%,如下圖示範:

extract_files

  • %USERPROFILE%\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin加入到環境變數Path,如下圖示範:

var_editor

add_var

文字版流程:

  1. 在「搜尋」中,搜尋並選取:系統 (控制台)
  2. 按一下進階系統設定連結。
  3. 按一下環境變數。在系統變數區段中,找到 PATH 環境變數並加以選取。按一下編輯。如果 PATH 環境變數不存在,請按一下新增。
  4. 在編輯系統變數 (或新增系統變數) 視窗中,指定 PATH 環境變數的值。按一下確定。按一下確定,將其餘的視窗全都關閉。

NOTE: %USERPROFILE%\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin 的順序是有差的,請確認是在最下方才是正確的。 若不是可以使用右側的 上移下移 調整順序。

測試編譯以及除錯

測試編譯

  1. vscode編輯器選擇開啟解壓縮檔案裡面的test資料夾,如下圖示範:

open_vscode

NOTE: 如果電腦上沒有 test 資料夾,需要在 %USERPROFILE% 中新增一個資料夾,名稱可以自由取。

  1. 信任開啟檔案,選擇Yes, I trust,如下圖示範:

trust_vscode

  1. 點擊左方test.cpp檔案後,在第5行左側按下去,會出現紅點,如下圖示範:

add_break_point

  1. 按下ctrl+shift+B按鍵,或上方terminal -> Run Build Task,如下圖示範:

run_build_task

  1. 此時上方會出現以下畫面,選擇中間選項使用 g++ 編譯檔案,如下圖示範:

build_hint

  1. 設置正確的話,此時下方會出現編譯成功完成的提示,如下圖示範:

build_successfully

測試除錯

  1. 完成前項測試編譯的所有流程

  2. 按下左方三角形按鈕後選擇Run and Debug,如下圖示範:

open_debugger

  1. 此時上方會依序出現相關提示,選擇預設的即可,如下圖示範:

debuger_hint-1 debuger_hint-2

  1. 若設定正確的話,會看到程式停留在選取的第5行,如下圖示範:

debuger_successful

Reference: