Try   HackMD
tags: Editor VSCode Shortcut Key GNU makefile C/C++ Code Runner C/C++ Makefile Project debug

從零開始設定 VSCode 環境 (Win 10)

你將會學到以下技能:

  1. 建立 C/C++ 的 VSCode 環境 (使用 MinGW-w64 編譯器)
  2. 建立 Makefile C/C++ 專案 (需先備 Makefile 基本常識)
  3. 設定快速鍵來編譯並執行 Makefile C/C++ 專案
  4. 建立 VSCode 的 Debug 環境

Step 1: Download Packages

Step 2: 設定環境變數

參考此教學

  • 快速打開系統內容視窗: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.

如何一鍵執行 makefile 專案

Step 1: Install Extension for C/C++

Essential

  • C/C++
  • Code Runner
    • Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
      Code-runner: Run In Terminal
    • Code-runner: Custom Command

      cd "$workspaceRoot" && make && .\main.exe
      check out more

  • C/C++ Makefile Project

Interface Optimization (Optional)

  • indent-rainbow
  • Material Icon Theme
  • Material Theme
  • Prettier - Code formatter
    • Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
      format on save
  • Bracket Pair Colorizer 2
  • Markdown Preview Enhanced

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)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Then how to create a makefile-based C/C++ project?

Start from 4:00

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

簡要步驟如下

  1. Open Command Palette: ctrl + shift + P

  2. Enter make ➜ Select C/C++ Make: INIT Project

  3. Edit 3 places in Makefile

CXXFLAGS = -std=c++11 -Wall # add "-g" when debugging
APPNAME = main
# Windows OS variables & settings DEL = rm
  1. Press ctrl + R 便可自動編譯並執行 makefile C/C++ Project


建立偵錯環境

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
How to DEBUG a makefile-based C/C++ project?

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

簡要步驟

  1. Press F5 ➜ Select C++ (GDB/LLDB).vscode\launch.json 會自動建立
  2. Modify launch.json
"program": "${workspaceFolder}\\main.exe",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
  1. Modify Makefile
CXXFLAGS = -std=c++11 -Wall -g
  1. 設定中斷點 ➜ Press F5 開始 DEBUG!
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →