天天看點

Mac OS - VSCode C++開發配置

VSCode 在 Mac 系統上使用 C++ 開發相信很多人都覺得配置比較難,配置教程也是各式各樣的。經過多次的嘗試,并參考了 官方教程,在原有資料的基礎上進行了補充和整理,這裡輸出了一個自己配置成功的方法。

⚠️注意:本文同樣适用于 Windows 系統。

預先安裝

1.安裝 VSCode

官方位址 Visual Studio Code on macOS

2.安裝插件

C/C++

,在擴充插件中搜尋 C++。

Mac OS - VSCode C++開發配置

3.确認已安裝

Clang

Clang

可能在你的電腦裡安裝過了,打開終端,輸入以下指令來确認是否安裝。

clang --version
           

Clang

未安裝,使用

xcode-select --install

進行安裝。

建立項目

1.建立一個 Hellow World 項目進行測試:

mkdir projects
cd projects
mkdir helloworld
cd helloworld
code .
           

上述步驟,建立了一個 Helloworld 項目,并在 VSCode 中打開。

2.建立一個 cpp 檔案

Mac OS - VSCode C++開發配置

3.建立一個檔案取名為 helloworld.cpp,将以下代碼粘貼進檔案中,并按

Command + S

進行儲存。

#include <iostream>
using namespace std;

int main()
{
	for(int i=0; i<=5; i++)
	   cout << "Hello World " << i << endl;
   return 0;
}
           

配置 C++ 相關設定檔案

我們需要生成三個檔案:

  • tasks.json (compiler build settings)
  • launch.json (debugger settings)
  • c_cpp_properties.json (compiler path and IntelliSense settings)

tasks.json 檔案

tasks.json 用來告訴 VSCode 如何編譯該程式,這裡會調用 Clang C++ 進行編譯。

這裡需要将 helloworld.cpp 檔案在編輯界面中打開,然後選擇菜單欄

Terminal

(終端) >

Configure Default Build Task

(配置預設生成任務),選擇下面選項。

Mac OS - VSCode C++開發配置

現在在 .vscode 檔案夾中,就生成了 tasks.json 檔案。将下面的代碼替換掉原來的内容。

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "clang++ build active file",
      "command": "/usr/bin/clang++",
      "args": [
        "-std=c++17",
        "-stdlib=libc++",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}
           

接下來,回到 helloworld.cpp 檔案,按

Ctrl + Shift + B

進行編譯或選擇

Terminal

(終端) >

Run Build Task

(運作任務)。

如果編譯成功,會在終端中顯示,并且可以在目錄中看到有一個 helloworld.dSYM 檔案夾。(這是用于 Debug 的,先不管它)

Mac OS - VSCode C++開發配置

launch.json 檔案

該檔案用于配置 VSCode 啟動 LLDB debug。

選擇

Run

>

Add Configuration

,并選擇 C++ (GDB/LLDB),再選擇clang++ build and debug active file

Mac OS - VSCode C++開發配置

現在在 .vscode 檔案夾中,就生成了 launch.json 檔案。其内容如下:

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "clang++ - Build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": true,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "lldb",
      "preLaunchTask": "clang++ build active file"
    }
  ]
}
           

參數解釋:

  • program

    表示你具體的想 Debug 的程式
    • 其中

      ${fileDirname}

      表示程式所在的檔案夾
    • ${fileBasenameNoExtension}

      表示想要 Debug的程式。不需要進行修改,對你想要運作的檔案進行 Debug 就能夠自動識别。
  • stopAtEntry

    設定為 true 時,會讓 debugger 在 main 函數處進行一次停頓。
  • preLaunchTask

    的值要和 task.json 中 label 的值一緻。

開始 Debug

回到 helloworld.cpp,這一步非常重要,因為 VSCode 會檢測你目前哪個視窗處于活躍狀态,并對其進行 **Debug。(活躍:你的界面中顯示的視窗,并且有光标)

使用

F5

或從菜單欄中選擇

Run

(運作) >

Start Debugging

(啟動調試)就能進行開始 Debug。