文末放了我的三個json文件配置,可以參考。
1.下載VS code
VS code已針對m1 芯片進行了適配,去官網下載VS code Apple Silicon版並安裝。
2.確保clang已安裝
在終端里輸入clang --version查看是否已安裝,若未安裝,輸入xcode-select --install讀完條款輸入agree安裝即可。
3.下載擴展
一共有三個擴展需要下載。
1.C/C++
2.C++ Intellisense
3.CodeLLDB
4.Chinese(中文插件,可選)
4.新建cpp文件
這里直接采用微軟官方文檔里的代碼。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
這個時候,在文件第10行輸入msg.應該是能看到VS code跳出assign之類的提示的。
5.配置文件
1.配置tasks.json文件
首先點擊終端---配置默認生成任務--- C/C++ clang++,會生成一個tasks.json文件,這是默認生成的,需要修改其中的args選項,添加一個"-std=c++17",修改后為
"args": [
"-g",
"${file}",
"-std=c++17",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
配置完后,會發現VS code提示語法錯誤,不要急,接下來就修正它。
2.配置c_cpp_properties.json文件
使用組合鍵shift+command+p(mac下shift就是fn鍵上面那個啦),調出C/C++:編輯配置(json),會自動生成一個c_cpp_properties.json文件。
將"compilerPath": "/usr/bin/clang",修改為"compilerPath": "/usr/bin/clang++",
將""cppStandard": "c++98",修改為"cppStandard": "c++17",
其實只是將編譯器修改為clang++,cpp標准修改為C++ 17.
3.編譯生成文件
這么配置完后,其實VS code還是會報兩個語法錯誤,不過這不要緊,這是因為還沒更新的緣故。
點擊終端---運行生成任務,運行完后會生成一個二進制文件,語法報錯也沒了,表示我們編譯成功了。
如果想測試的話,新建一個終端,使用./你的二進制文件名,即可看到輸出結果。
4.配置launch.json文件
點擊運行---添加配置---C++(GDB/LLDB)---clang++,會生成一個launch.json文件。
將"type": "cppdbg",修改為"type": "lldb",
至此,所有文件就配置完了。
5.調試
在文件的第10行放一個斷點,點擊運行---啟動調試,就可以看到各種變量了。
我的文件配置
以下是我的三個json文件配置代碼。
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ 生成活動文件",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-std=c++17",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "編譯器: /usr/bin/clang++"
}
]
}
c_cpp_properties.json:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang++",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-arm64"
}
],
"version": 4
}
launch.json:
{
// 使用 IntelliSense 了解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "clang++ - 生成和調試活動文件",
"type": "lldb",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang++ 生成活動文件"
}
]
}
