Visual studio Code的C/C++开发环境搭建
VS Code
Visual Studio Code(簡稱VS Code)是一個由微軟開發,同時支持Windows 、 Linux和macOS等操作系統且開放源代碼的代碼編輯器,它支持測試,并內置了Git 版本控制功能,同時也具有開發環境功能,例如代碼補全(類似于 IntelliSense)、代碼片段和代碼重構等。該編輯器支持用戶個性化配置,例如改變主題顏色、鍵盤快捷方式等各種屬性和參數,同時還在編輯器中內置了擴展程序管理的功能
- 官網下載 https://code.visualstudio.com/
- C/C++插件官方文檔 https://code.visualstudio.com/docs/languages/cpp
C/C++環境配置
在window下安裝C/C++環境以及VSCode,并能夠簡單初步使用,本測試環境如下
- VS code 1.52
- Win 10
- msys2 / MinGW64
- CentOS7
環境準備
- 安裝gcc等相關編譯鏈,參照之前的MSYS2開發環境搭建,并將相應的路徑加入PATH環境變量中,比如:D:\msys64\mingw64\bin
- 官網下載VS code,雙擊安裝即可
- 啟動VScode,Ctrl + Shift + X切換到插件窗口,搜C++, 安裝C/C++,C++ Intellisense以及`Chinese (Simplified) Language Pack for Visual Studio Code
等插件
使用實例
-
在桌面新建demo目錄
-
【文件】-【打開文件夾】選擇創建的demo目錄
-
【文件】-【新建文件】demo.cpp
#include <iostream> #include <vector> #include <string>int main(int argc, char *argv[]) {std::cout << "Hello World" << std::endl;std::vector<std::string> cities{"JiNan", "BeiJing", "ShenZhen"};for (auto& city : cities){std::cout << city << std::endl;}return 0; } -
在demo.cpp編輯界面,Ctrl + Shift + P,輸入 C/C++,點擊 C/C++: 編輯配置 (UI),配置完成后會自動生成.vscode/c_cpp_properties.json
- 編譯器路徑,如:D:/msys64/mingw64/bin/g++.exe
- 編譯器參數
- IntelliSense 模式,我選的 gcc-x64
- 包含路徑,添加非標準化路徑下的include路徑
-
在demo.cpp編輯界面,【終端】-【配置默認生成任務】在彈出的選項卡中選擇要用做默認生成任務的任務,我這里選擇的是C/C++: g++.exe build active file, VSCode會自動生成.vscode/tasks.json文件,文件內容如下
{"version": "2.0.0","tasks": [{"type": "cppbuild","label": "C/C++: g++.exe build active file","command": "D:\\msys64\\mingw64\\bin\\g++.exe","args": ["-g","${file}","-o","${fileDirname}\\${fileBasenameNoExtension}.exe"],"options": {"cwd": "D:\\msys64\\mingw64\\bin"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true},"detail": "compiler: D:\\msys64\\mingw64\\bin\\g++.exe"}] } -
在demo.cpp的編輯界面,【終端】-【運行生成任務】或者快捷鍵Ctrl + Shift + B,執行編譯任務生成demo.exe可執行程序
-
在demo.cpp的編輯界面,【運行】-【添加配置】在彈出的選項卡中選擇環境 C++(GDB/LLDB),選在配置,我這里選的第一個 **g++.exe - 生成和調試活動文件 **,自動生成.vscode/launch.json
{// 使用 IntelliSense 了解相關屬性。 // 懸停以查看現有屬性的描述。// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "g++.exe - 生成和調試活動文件","type": "cppdbg","request": "launch","program": "${fileDirname}\\${fileBasenameNoExtension}.exe","args": [],"stopAtEntry": false,"cwd": "D:/msys64/mingw64/bin","environment": [],"externalConsole": false,"MIMode": "gdb","miDebuggerPath": "D:\\msys64\\mingw64\\bin\\gdb.exe","setupCommands": [{"description": "為 gdb 啟用整齊打印","text": "-enable-pretty-printing","ignoreFailures": true}],"preLaunchTask": "C/C++: g++.exe build active file"}] } -
在demo.cpp編輯界面,在行號前面出現小手,點擊設置斷點;【運行】-【啟動調試】即可調試程序
基于 VSCode 的遠程開發平臺
- VS Code Remote Development
- Remote development over SSH
The Remote Development extension pack includes three extensions. See the following articles to get started with each of them:
- Remote - SSH - Connect to any location by opening folders on a remote machine/VM using SSH.
- Remote - Containers - Work with a sandboxed toolchain or container-based application inside (or mounted into) a container.
- Remote - WSL - Get a Linux-powered development experience in the Windows Subsystem for Linux.
本文簡單闡述通過SSH連接linux開發環境進行開發的過程
環境準備
- 準備好的Linux開發環境
- 本地安裝與OpenSSH兼容的ssh客戶端
- VS Code安裝Remote - SSH插件
Win10下安裝與OpenSSH兼容的ssh客戶端安裝有多種方式:
- Windows10已經預裝了OpenSSH客戶端 【設置】-【應用】-【應用和功能】-【管理可選功能】,查看OpenSSH客戶端是否已安裝,沒有就點擊 添加功能進行添加;
- Git自帶ssh客戶端,可以安裝https://git-scm.com/download后,將Git的bin加入PATH環境變量,如路徑D:\Program Files\Git\usr\bin;
- 搭建MSYS2開發環境,并安裝ssh客戶端
安裝ssh客戶端以后,配置SSH免密登錄
參考
- 手把手教你配置VS Code遠程開發工具,工作效率提升N倍
總結
以上是生活随笔為你收集整理的Visual studio Code的C/C++开发环境搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IndexOptions类说明
- 下一篇: 【转载保存】mysql不设置主键使用自增