###### tags: `Editor` `VSCode` `Shortcut Key` `GNU` `makefile` `C/C++` `Code Runner` `C/C++ Makefile Project` `debug` [ToC] # 從零開始設定 VSCode 環境 (Win 10) ::: info 你將會學到以下技能: 1. 建立 C/C++ 的 VSCode 環境 (使用 MinGW-w64 編譯器) 2. 建立 **Makefile C/C++ 專案** (需先備 Makefile 基本常識) 3. 設定**快速鍵**來編譯並執行 **Makefile C/C++ 專案** 4. 建立 VSCode 的 Debug 環境 ::: ## Step 1: Download Packages - [Visual Studio Code] - [MinGW-w64] - 安裝路徑設定在 C:\ 根目錄較為方便 (往後範例將以此路徑為主) ::: warning * [Ubuntu 請使用 dpkg 安裝(snap 中文無法輸入)](https://askubuntu.com/questions/616075/how-do-i-install-visual-studio-code) ::: [visual studio code]: https://code.visualstudio.com/download [mingw-w64]: https://sourceforge.net/projects/mingw-w64/ ## Step 2: 設定環境變數 ### [:link: 參考此教學] - #### 快速打開**系統內容**視窗:run: **++sysdm.cpl++** ➜ 進階 ➜ 環境變數 ➜ 系統變數 ➜ Path - #### 新增以下兩筆路徑 - For g++: ==C:\MinGW\bin== - For make: ==C:\MinGW\msys\1.0\bin== - #### 打開 cmd 驗證是否安裝成功,驗證指令如下 ``` > g++ --version g++ (MinGW.org GCC Build-20200227-1) 9.2.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ``` ``` > make --version GNU Make 3.81 Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ``` [:link: 參考此教學]: https://ithelp.ithome.com.tw/articles/10190235 --- # 如何一鍵執行 makefile 專案 ## Step 1: Install Extension for C/C++ ### Essential - **C/C++** - **Code Runner** - :ballot_box_with_check: Code-runner: **_Run In Terminal_** - Code-runner: **_Custom Command_** > cd "$workspaceRoot" && make && .\main.exe > [check out more...] - **C/C++ Makefile Project** [check out more...]: https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner ### Interface Optimization (Optional) - **indent-rainbow** - **Material Icon Theme** - **Material Theme** - **Prettier - Code formatter** - :ballot_box_with_check: format on save - ~~**Bracket Pair Colorizer 2**~~ - **Markdown Preview Enhanced** :::info **Build-in Settings** - Files: Auto Save `onFocusChange` - Editor › Bracket Pair Colorization: Enabled ::: ## Step 2: VSCode settings & Keyboard Shortcuts ### Keyboard Shortcuts | Command | Keybinding | | -------------------- | :--------------- | | Run Code | Ctrl + Shift + R | | Run Custom Command | Ctrl + R | | File: Save all | Ctrl + Shift + S | | File: Save As... | Ctrl + K S | | File: Open Recent... | (None) | ## :memo: Then... how to create a makefile-based C/C++ project? ### Start from 4:00 {%youtube whQQF4kVjPY?t=240 %} ### 簡要步驟如下 1. #### Open Command Palette: `ctrl + shift + P` 2. #### Enter `make` ➜ Select `C/C++ Make: INIT Project` 3. #### Edit 3 places in `Makefile` ```makefile=7 CXXFLAGS = -std=c++11 -Wall # add "-g" when debugging ``` ```makefile=11 APPNAME = main ``` ```makefile=23 # Windows OS variables & settings DEL = rm ``` 4. #### Press ==`ctrl + R`== 便可自動編譯並執行 makefile C/C++ Project --- # 建立偵錯環境 ## :bug: How to DEBUG a makefile-based C/C++ project? {%youtube 9VpiGwp8Vos%} ### 簡要步驟 1. Press `F5` ➜ Select `C++ (GDB/LLDB)`➜ `.vscode\launch.json` 會自動建立 2. Modify `launch.json` ```json=11 "program": "${workspaceFolder}\\main.exe", ``` ```json=18 "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe", ``` 3. Modify `Makefile` ```makefile=7 CXXFLAGS = -std=c++11 -Wall -g ``` 4. 設定中斷點 ➜ Press `F5` 開始 DEBUG! :bug: <!-- ### Tutorial 2 (遺棄別看) {%youtube ABVeAXcRIJg%} #### 簡要步驟 1. Open Command Palette: `ctrl + shift + P` 2. Enter `c++ edit` ➜ Select `C/C++: Edit Configurations (UI)` 3. Modify following settings | Settings | Properties | | ----------------- |:-------------------- | | Compiler Path | C:/MinGW/bin/g++.exe | | IntelliSense mode | gcc-x64 | 4. `.vscode\c_cpp_properties.json` 會自動建立 -->