打算用C/C++把基本的數據結構與算法實現一遍, 爲考研作準備, 由於只是想實現算法和數據結構, 就不太想用VisualStudio, 感受VSCode不錯, 遂在網上找了一些教程, 結合本身的需求, 配置一下開發環境.html
CMake
CMake是一個跨平臺的自動化建構系統,它使用一個名爲 CMakeLists.txt 的文件來描述構建過程;前端
官網下載安裝, 傻瓜式操做;linux
記得把安裝目錄下的bin文件添加到系統環境變量, 這個能夠在安裝的時候勾選, 勾選了就不用本身添加了;ios
檢測是否安裝成功:
c++
MinGW
MinGW即Minimalist GNU For Windows,是將GNU開發工具移植到Win32平臺下的產物,是一套Windows上的GNU工具集。簡單說,MinGW是一個編譯環境; 至關於linux下的GCC;算法
官網下載連接;shell
安裝好將安裝目錄下的bin文件夾的路徑添加到環境變量:編程
Clang
相似GCC的編譯器, 它的目標是乾死GCC(網上看到別人這麼說的).json
維基百科:
Clang(發音爲/ˈklæŋ/相似英文單字clang[1]) 是一個C、C++、Objective-C和Objective-C++編程語言的編譯器前端。它採用了LLVM做爲其後端,並且由LLVM2.6開始,一塊兒發佈新版本。它的目標是提供一個GNU編譯器套裝(GCC)的替代品,支持了GNU編譯器大多數的編譯設置以及非官方語言的擴展。做者是克里斯·拉特納(Chris Lattner),在蘋果公司的贊助支持下進行開發,而源代碼許但是使用類BSD的伊利諾伊大學厄巴納-香檳分校開源碼許可。windows
VSCode中能夠用它來提供智能提示;
傻瓜式安裝, 安裝好把安裝目錄下的bin文件夾的路徑添加到環境變量;
如圖, 安裝好圖中的插件:
#include <iostream> using namespace std; int main(int argc, char* argv[]) { int a = 89; int b = a++; char buf[10] ; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "Hello..."; cin.get(); //防止閃屏 return 0; }
cmake_minimum_required(VERSION 3.11) project(VSCode_Cpp_CMake) # 代碼路徑 aux_source_directory(. DIR_TOOT_SRCS) # debug 模式 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g") # 生成可執行的文件 add_executable(VSCode_Cpp_CMake ${DIR_TOOT_SRCS})
鍵盤按下: ctrl+shift+p
{ "configurations": [ { "name": "CMake", "includePath": [ "${workspaceFolder}/**" ], "defines": [ "_DEBUG", "UNICODE", "_UNICODE" ], "windowsSdkVersion": "10.0.18362.0", "compilerPath": "C:/MinGW/bin/g++.exe", // 根據本身的安裝目錄肯定 "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "clang-x64", // 注意修改, 提供智能提示 "configurationProvider": "vector-of-bool.cmake-tools" } ], "version": 4 }
按快捷鍵: ctrl+shift+p
按下圖操做:
這裏操做好就能夠進行編譯生成exe文件了:
使用gdb進行調試.
快捷鍵: F5
-> C++ (GDB/LLDB)
-> g++.exe build and debug actvive file
vscode 會自動生成一個默認的 launch.json 和 task.json
編輯launch.json和task.json, 注意加了註釋的地方都是要修改的;
task.json
{ "tasks": [ { "type": "shell", "label": "cmake build active file", // 任務名稱 "command": "cmake --build ${workspaceFolder}\\build --config Debug --target all -- -j 10", // cmake指令 "args": [ ], // 指令參數 "options": { "cwd": "C:/MinGW/bin" } } ], "version": "2.0.0" }
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": "(gdb) Launch", // default: g++.exe build and debug active file "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}\\build\\Demo.exe", // 可執行文件所在的路徑, Demo= 替換成本身的項 "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, // 顯示獨立控制檯窗口 "MIMode": "gdb", "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "cmake build active file" // 執行cmake編譯任務, 再task.json中定義的 } ] }
編譯 快捷鍵: F7
調試 快捷鍵: F5