0.下載VS code后安裝以下插件:
- C\C++
- C\C++ Clang Command Adapter
- CodeLLDB(用來debug)
- code runner (用來編譯)
安裝好插件后,要配置文件,也就是.vscode文件夾下的三個json文件。
(每次新建項目文件夾都要在對應的文件夾下配置一遍)
1.c_cpp_properties.json(編譯器路徑和智能感知設置)
shift+command+P -> C/C++ 編輯配置(JSON)配置文件c_cpp_properties.json
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
2. tasks.json(編譯器生成設置)
command+shift+B 進行編譯,此時我們還沒有要編譯的文件,這時會跳出來一個小窗口,一直點到other那里。這時有一個tasks.json生成。(如果不出來也可以手動創建,即直接在.vscode文件夾下創建一個然后把對應的內容粘貼進去保存) ,用以下代碼替換生成的task.json文件。
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with Clang",
"type": "shell",
"command": "clang++",
"args": [
"${file}",
"-std=c++11",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out",
"-g",
"--debug"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
3.launch.json(調試器設置)
然后選LLDB
4.測試
#include<iostream>
using namespace std;
int main(){
cout<<"hello,c++"<<endl;
return 0;
}
然后就可以愉快地運行cpp文件啦。
(但是用cin的話程序會一直運行但是也找不到輸入的地方)