Mac上利用VScode配置c/c++開發環境
哭遼,Typora里面最好不要插入表情,不然保存會閃退
- 首先你要有一個vscode
- 在擴展里面下載c/c++
第一步
⬆+com+p 打開命令模式:選擇c/c++: 編輯配置(edit configuration)
然后再自動生成的.vscode目錄,打開c_cpp_properties.json。利用老哥的文件示例:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/Library/Developer/CommandLineTools/usr/include/c++/v1",
"/usr/local/include",
"/Library/Developer/CommandLineTools/usr/lib/clang/11.0.0/include",
"/Library/Developer/CommandLineTools/usr/include"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks",
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
將要用到的庫添加到includePath里面
第二步:
[⇧⌘P]打開命令模式,選擇[Tasks: Configure Task]命令,選擇的模板為MSBuild,回車后會自動在.vscode目錄下生成一個tasks.json文件,下面給出我的文件示例:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build c++",
"type": "shell",
"command": "g++",
"args": [
"${file}",
"-std=c++17",
"-g",
"-Wall",
"-lm",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out"
],
"group": "build",
"presentation": {
"reveal": "silent",
"panel": "shared",
"echo": true,
"focus": false,
"showReuseMessage": true,
"clear": false
},
"problemMatcher": "$gcc"
},
{
"label": "run c++",
"type": "shell",
"dependsOn": "build c++",
"command": "${fileDirname}/${fileBasenameNoExtension}.out",
"presentation": {
"focus": true
},
"group": "test"
}
]
}
- 很多參數咱也不懂,先照抄吧
該文件其實就是一個命令行構建工具。
把運行程序時在終端輸入的命令和參數對於"command"和"args"的值
輸入shift+command+b,便可構建成功,生成可執行文件「文件名」.out
第三步:
[⇧⌘P]打開命令模式,選擇[Debug: Open launch.json]命令,選擇的模板為C/C++,回車后會自動在.vscode目錄下生成一個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": "c/c++ Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true, //true代表會彈出外面終端,顯示
"MIMode": "lldb",
"preLaunchTask": "build c++",
"logging": {//這段會添加一大段沒用的建議刪除
"trace": true,
"traceResponse": true,
"engineLogging": true
}
}
]
}
完成這三步C++開發環境就配置好了,接下來就可以編譯,運行,調試C++程序了
[⇧⌘B]是編譯程序,[⇧⌘R]是運行程序,如果安裝了插件『Code Runner』可以直接運行程序
如果需要調試,那就按F5,進入調試模式即可
[⇧⌘B]會編譯生成.out文件,再按[⇧⌘R]會在終端窗口顯示運行結果 或者 進行交互。
參考文章
在mac上使用vscode創建第一個C++項目
Mac在VSCode中搭建C/C++環境