Visual Studio Code 是一款功能强大、界面优美的编辑器。下面介绍在Mac下基于Visual Studio Code搭建C/C++ IDE。
安装 Visual Studio Code
- 可以通过 brew 直接安装
brew cask install visual-studio-code
- 也可以直接下载安装包安装,下载地址
安装C/C++ 插件
只需要安装下面两个插件即可:
- C/C++
- C/C++ Clang Command Adapter
配置编译task,task.json
在当前文件是C++的情况下,编译选项的配置是放在 .vscode/tasks.json 里面的,主要用来告诉VS Code如何Compile/Build 程序,附上我的task.json 配置
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "g++ build cpp program",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-std=c++11",
"-stdlib=libc++",
"-o",
"${fileBasenameNoExtension}",
"${file}"
],
"group": {
"kind": "build",
"isDefault": true
}
},
]
}
注: 如果要使用C++ 11, 则需要增加 "-std=c++11"、"-stdlib=libc++" 这两个选项
如何执行编译好的文件, 配置 launch.json
.vscode/launch.json 文件告诉VS Code 如何运行编译好的文件,附上我的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": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "g++ build cpp program"
}
]
}
注意: tasks.json的"label"参数值和launch.json的"preLaunchTask"参数值必须一致,否则可能报“launch:program "xxxx" does not exist”
运行程序
点击 Debug->Start Debugging 即可开始调试任务