VS code本身是一个编辑器,如果需要调试之类的功能需要自己安装一些插件,并且配置相关的json文件。
linux 环境下,g++和clang都可以作为C++的编译器,我选择使用的是g++。
首先是插件选择:
(1) C/C++ 微软自带的C/C++插件。
(2) C/C++ Clang Command Adapter:提供静态检测(Lint)
(3) Code Runner:右键即可编译运行单文件(默认编译命令没有使用c++11及以上版本,如果需要添加,可以看下文)
(4) Bracket Pair Colorizer:彩虹花括号
(5) Include Autocomplete:提供头文件名字的补全
这些是我选择的一些插件
在下载完一些插件之后,需要新建一个文件夹作为项目的根目录
新建的目录下,啥文件也没有,这时候需要创建一个.vscode的文件夹,该文件夹是存放json的配置文件的,其中有两个文件必不可少,一个是launch.json,另一个是tasks.json
其中,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": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
tasks的配置为:
{
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-std=c++11",
"-lpthread"
],
"options": {
"cwd": "/usr/bin"
}
}
],
"version": "2.0.0"
}
其中,如果编译的时候需要其他的命令,例如 -std=c++11 之类的,可以添加在tasks的args中。
.vscode至少需要以上两个文件,setting.json 不一定需要,添加完的效果如下
CodeRunner 默认没有使用c++11 及以上的版本,如果需要添加的话,找到下图中的位置
然后添加以下代码
"C_Cpp.default.cppStandard": "c++11",
"code-runner.executorMap": {
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt -std=c++11 -lpthread && $dir$fileNameWithoutExt"
}
如果需要其他名利,只需要该cpp 中的编译命令即可