在mac上使用vscode創建第一個C++項目
- 2020 年 12 月 30 日
- 筆記
- mac下c++運行的方法
//blog.csdn.net/bujidexinq/article/details/106539523
準備工作:
安裝好vscode
安裝插件『C/C++』
正式開始:
首先是創建一個空的文件夾(比如文件夾為test),然後在其中新建一個.cpp文件(比如文件為hello.cpp)打開vscode打開test文件夾作為工作目錄,接下來用三步配置好C++開發環境
第一步:
[⇧⌘P]打開命令模式,選擇[C/Cpp: Edit Configurations(JSON)]命令,回車後會自動生成一個.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
-
}
第二步:
[⇧⌘P]打開命令模式,選擇[Tasks: Configure Task]命令,選擇的模板為MSBuild,回車後會自動在.vscode目錄下生成一個tasks.json文件,下面給出我的文件示例:
-
{
-
// See //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”
-
}
-
]
-
}
第三步:
[⇧⌘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: //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,
-
“MIMode”: “lldb”,
-
“preLaunchTask”: “build c++”,
-
“logging”: {
-
“trace”: true,
-
“traceResponse”: true,
-
“engineLogging”: true
-
}
-
}
-
]
-
}
完成這三步C++開發環境就配置好了,接下來就可以編譯,運行,調試C++程式了
[⇧⌘B]是編譯程式,[⇧⌘R]是運行程式,如果安裝了插件『Code Runner』可以直接運行程式
如果需要調試,那就按F5,進入調試模式即可
好了,這樣就可以順利地在vscode上進行C++開發啦,趕快行動起來吧~