1. 程式人生 > >使用 Visual Studio Code 編譯和除錯 C++ 檔案

使用 Visual Studio Code 編譯和除錯 C++ 檔案

前言

現在有很多的程式碼編輯器都可以提供命令列功能來 Build C++ 檔案,例如我之前用過的 Sublime 和 Notepad++。這兩款軟體非常小巧而且不用付費即可使用。但是很遺憾其很難使用偵錯程式對程式碼進行除錯。於是我們找到了 Visual Studio Code (VSC)這款工具,雖然其安裝大小要幾百兆,但是配置好了之後真的非常好用,比動輒十幾G的 Visual Studio Community 輕便了太多。廢話不多說,接下來我們看看怎麼配置的。官方教程是英文版的但是顯然會更加準確,偷懶的話就繼續看我寫的版本吧。

環境準備

首先安裝MinGW-w64,官方網站上不提供下載,需要去

Sourceforge 上下載。下載和安裝教程這裡不贅述了,具體可百度。注意配置好使用者環境變數中的 Path。驗證方法為開啟 cmd 輸入 gcc 看是否是不識別的命令。若沒有配置好,那麼這個命令是不識別的,即 Bad command。

然後需要安裝正主 visual studio code 了,這個下載更簡單,直接百度很容易就能找到下載位置

環境配置

首先需要先安裝官方 C++ 外掛。開啟 VSC,點選左方由上到下第五個圖示,搜尋 C++,第一個就是,點選安裝,安裝完了之後過載。
新建一個資料夾,作為工作資料夾。在這個資料夾內建立一個 cpp 檔案,作為除錯程式碼。懶的話直接複製以下程式碼:

#include <iostream>
#include <aaa>

int main() {
    int a;

    std::cout << "Test." << std::endl;
    std::cin >> a;

    return 0;
}

注意以上有一行程式碼是錯誤的,這是我故意寫出來的。將程式碼複製粘貼後記得儲存(Ctrl + s)。
這是錯誤的程式碼行,將滑鼠移向這行程式碼會蹦出一個黃色的燈泡,點選它選擇 Edit “includePath” setting.

#include <aaa>

幹完這步後,VSC 會自動在工作目錄下建立 .vscode 資料夾。注意,在 VSC 中要開啟資料夾,選擇你建立的工作目錄。
在 .vscode 中新建 launch.json 檔案,這是配置 VSC 除錯的檔案,直接複製以下程式碼:

{
    // 使用 IntelliSense 瞭解相關屬性。 
    // 懸停以檢視現有屬性的描述。
    // 欲瞭解更多資訊,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "internalConsoleOptions": "neverOpen",
            "MIMode": "gdb",
            "miDebuggerPath": "gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "Compile"
        }
    ]
}

再建立 tasks.json 檔案,這個檔案用於配置編譯選項。程式碼如下:

// https://code.visualstudio.com/docs/editor/tasks
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile",
            "command": "g++",
            "args": [
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe",
                "-g",
                "-Wall",
                "-static-libgcc",
            ],
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            },
            "problemMatcher": "$gcc"
        }
    ]
}

最後一步,建立檔案 c_cpp_properties.json,不過這個檔案系統應該在之前已經為你建立好了。這個檔案主要用於配置 include 資料夾。這裡不要全部複製貼上,覆蓋對應部分即可!

        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}",
                "C:\mingw-w64\x86_64-7.2.0-posix-seh-rt_v5-rev1\mingw64\include"
                "C:\mingw-w64\x86_64-7.2.0-posix-seh-rt_v5-rev1\mingw64\x86_64-w64-mingw32\include" //這兩行需要替換為自己的 mingw-w64安裝資料夾。
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "path": [
                    "${workspaceFolder}",
                    "C:\mingw-w64\x86_64-7.2.0-posix-seh-rt_v5-rev1\mingw64\include"
                    "C:\mingw-w64\x86_64-7.2.0-posix-seh-rt_v5-rev1\mingw64\x86_64-w64-mingw32\include" //這兩行需要替換為自己的 mingw-w64安裝資料夾。
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            },
            "cStandard": "c11",
            "cppStandard": "c++17"
        }

到這裡就大功告成了,除錯程式試試看?