在Mac / Windows上搭建VS Code C/C++開發環境
阿新 • • 發佈:2020-12-27
目錄
MacOS Catalina
- 開啟App Store安裝Xcode直至完成
- 安裝命令列工具
xcode-select --install
- 安裝VS Code
brew cask install visual-studio-code
- VS Code配置
- 安裝"C/C++ Extension",該過程會自動安裝其依賴的相關外掛
- 開啟一個工作空間,在介面輸入"Command+Shift+P",執行"C/C++ Edit Configurations (JSON)"
- 在開啟的配置檔案中的
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"
- 在開啟的配置檔案中的
macFrameworkPath
中新增
"/System/Library/Frameworks", "/Library/Frameworks"
- 在介面輸入"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" } } ] }
- 在介面輸入"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" } ] }
- 新建cpp/hello.cpp檔案,輸入以下內容,看看智慧程式碼提示是否生效
#include <iostream> using namespace std; int main() { cout<<"Hello"<<endl; }
- 測試執行,在執行視窗可看到剛配置的"C/C++ Launch"任務,執行將彈出Terminal顯示Hello以及一些輔助資訊
- 測試除錯,在cpp/hello.cpp第5行新增斷點,將看到斷點除錯效果
Windows 10
在Windows 10上搭建VS Code C/C++開發環境(基於WSL服務)
- 按Win鍵搜尋功能"Windows Features"
- 在彈出視窗中勾選"Windows Subsystem for Linux"項
- 開啟Microsoft Store搜尋Ubuntu並開始安裝(也可憑個人愛好選用其他的發行版)
- 安裝成功後進入啟動初始化
- 初始化完成後,進入系統按需修改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
- 安裝開發所需軟體
sudo apt update
sudo apt install gcc gdb
- 從官網下載安裝VSCode
- VS Code配置
- 安裝"Remote WSL"外掛,安裝後會自動連線WSL並在WSL上下載安裝VSCode Server
- 開啟一個工作空間,安裝"C/C++ Extension"
- 外掛安裝後,正常情況下會自動在空間下配置好tasks.json和launch.json
- 可進行執行和除錯
至此,環境配置結束。