1. 程式人生 > 實用技巧 >在Mac / Windows上搭建VS Code C/C++開發環境

在Mac / Windows上搭建VS Code C/C++開發環境

目錄

MacOS Catalina

  1. 開啟App Store安裝Xcode直至完成
  2. 安裝命令列工具 xcode-select --install
  3. 安裝VS Code brew cask install visual-studio-code
  4. VS Code配置
    1. 安裝"C/C++ Extension",該過程會自動安裝其依賴的相關外掛
    2. 開啟一個工作空間,在介面輸入"Command+Shift+P",執行"C/C++ Edit Configurations (JSON)"
    3. 在開啟的配置檔案中的includePath中新增
    "/Library/Developer/CommandLineTools/usr/include/c++/v1",
    "/usr/local/include",
    "/Library/Developer/CommandLineTools/usr/lib/clang/12.0.0/include",
    "/Library/Developer/CommandLineTools/usr/include"
    
    1. 在開啟的配置檔案中的macFrameworkPath中新增
    "/System/Library/Frameworks",
    "/Library/Frameworks"
    
    1. 在介面輸入"Command+Shift+P",執行"Tasks: Configure Task",編輯內容如下
    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "C++: clang++ build active file",
                "command": "clang++",
                "type": "shell",
                "args": [
                    "-g",
                    "${file}",
                    "-std=c++11",
                    "-o",
                    "${fileDirname}/${fileBasenameNoExtension}"
                ],
                "presentation": {
                    "echo": true,
                    "reveal": "always",
                    "focus": false,
                    "panel": "shared"
                }
            }
        ]
    }
    
    1. 在介面輸入"Command+Shift+P",執行"Debug: Open launch.json",編輯內容如下
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "C/C++ Launch active file",
                "type": "cppdbg",
                "request": "launch",
                "program": "${fileDirname}/${fileBasenameNoExtension}",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "environment": [],
                "externalConsole": false,
                "MIMode": "lldb",
                "preLaunchTask": "C++: clang++ build active file"
            }
        ]
    }
    
    1. 新建cpp/hello.cpp檔案,輸入以下內容,看看智慧程式碼提示是否生效
    #include <iostream>
    
    using namespace std;
    int main() {
        cout<<"Hello"<<endl;
    }
    
    1. 測試執行,在執行視窗可看到剛配置的"C/C++ Launch"任務,執行將彈出Terminal顯示Hello以及一些輔助資訊
    2. 測試除錯,在cpp/hello.cpp第5行新增斷點,將看到斷點除錯效果

Windows 10

在Windows 10上搭建VS Code C/C++開發環境(基於WSL服務)

  1. 按Win鍵搜尋功能"Windows Features"
  2. 在彈出視窗中勾選"Windows Subsystem for Linux"項
  3. 開啟Microsoft Store搜尋Ubuntu並開始安裝(也可憑個人愛好選用其他的發行版)
  4. 安裝成功後進入啟動初始化
  5. 初始化完成後,進入系統按需修改APT源
# 備份配置檔案
sudo cp -a /etc/apt/sources.list /etc/apt/sources.list.bak
# 使用國內源(以華為映象源為例)
sudo sed -i "s@http://.*archive.ubuntu.com@http://mirrors.huaweicloud.com@g" /etc/apt/sources.list
sudo sed -i "s@http://.*security.ubuntu.com@http://mirrors.huaweicloud.com@g" /etc/apt/sources.list
  1. 安裝開發所需軟體
sudo apt update
sudo apt install gcc gdb
  1. 官網下載安裝VSCode
  2. VS Code配置
    1. 安裝"Remote WSL"外掛,安裝後會自動連線WSL並在WSL上下載安裝VSCode Server
    2. 開啟一個工作空間,安裝"C/C++ Extension"
    3. 外掛安裝後,正常情況下會自動在空間下配置好tasks.json和launch.json
    4. 可進行執行和除錯

至此,環境配置結束。